Some guidance needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bostonmegarocker

    Some guidance needed

    Hi to all?

    For the past couples of weeks I have been programming to make a
    program that reads a binary file and produces the equivalent text
    file.

    The idea is that each byte in the file represents a temperature that
    can only go from 0 to 255. So I start by reading the file in to a
    character array, and the do an int casting to obtain the value of each
    file, so my code is something like this:

    ifstream inFile("binary. dat");
    ofstream outFile("binary .txt");

    inFile.seekg(0, ios::end);
    int size = inFile.tellg();
    inFile.seekg(0, ios::beg);
    char charArray[size];

    inFile.read(cha rArray, size);

    for(int= 0; i<size; i++)
    {
    outFile << (int)charArray[i] <<endl;
    }

    .... etc ...

    I expected the output to range from 0 to 255, but I get values from
    -128 to 127!!? and if I use (unsigned int) instead of (int) the output
    is even worst.

    Is there a better way to see what is the integer equivalent that each
    byte holds ?

    Regards,
    Alexis
    _______________ ________
    bostonmegarocke r@yahoo.com
  • Min

    #2
    Re: Some guidance needed

    I assumed that 0-255 means, you don't have negative value.
    Then try "unsigned char"

    "bostonmegarock er" <bostonmegarock er@hotmail.com> wrote in message
    news:6cb461a8.0 306301205.11628 021@posting.goo gle.com...[color=blue]
    > Hi to all?
    >
    > For the past couples of weeks I have been programming to make a
    > program that reads a binary file and produces the equivalent text
    > file.
    >
    > The idea is that each byte in the file represents a temperature that
    > can only go from 0 to 255. So I start by reading the file in to a
    > character array, and the do an int casting to obtain the value of each
    > file, so my code is something like this:
    >
    > ifstream inFile("binary. dat");
    > ofstream outFile("binary .txt");
    >
    > inFile.seekg(0, ios::end);
    > int size = inFile.tellg();
    > inFile.seekg(0, ios::beg);
    > char charArray[size];
    >
    > inFile.read(cha rArray, size);
    >
    > for(int= 0; i<size; i++)
    > {
    > outFile << (int)charArray[i] <<endl;
    > }
    >
    > ... etc ...
    >
    > I expected the output to range from 0 to 255, but I get values from
    > -128 to 127!!? and if I use (unsigned int) instead of (int) the output
    > is even worst.
    >
    > Is there a better way to see what is the integer equivalent that each
    > byte holds ?
    >
    > Regards,
    > Alexis
    > _______________ ________
    > bostonmegarocke r@yahoo.com[/color]


    Comment

    • bostonmegarocker

      #3
      Re: Some guidance needed

      Thanks Min,
      That worked for me. I really appreciate your help.
      - alexis

      "Min" <nobody@home.co m> wrote in message news:<fY0Ma.327 717$ro6.7915627 @news2.calgary. shaw.ca>...[color=blue]
      > I assumed that 0-255 means, you don't have negative value.
      > Then try "unsigned char"
      >
      > "bostonmegarock er" <bostonmegarock er@hotmail.com> wrote in message
      > news:6cb461a8.0 306301205.11628 021@posting.goo gle.com...[color=green]
      > > Hi to all?
      > >
      > > For the past couples of weeks I have been programming to make a
      > > program that reads a binary file and produces the equivalent text
      > > file.
      > >
      > > The idea is that each byte in the file represents a temperature that
      > > can only go from 0 to 255. So I start by reading the file in to a
      > > character array, and the do an int casting to obtain the value of each
      > > file, so my code is something like this:
      > >
      > > ifstream inFile("binary. dat");
      > > ofstream outFile("binary .txt");
      > >
      > > inFile.seekg(0, ios::end);
      > > int size = inFile.tellg();
      > > inFile.seekg(0, ios::beg);
      > > char charArray[size];
      > >
      > > inFile.read(cha rArray, size);
      > >
      > > for(int= 0; i<size; i++)
      > > {
      > > outFile << (int)charArray[i] <<endl;
      > > }
      > >
      > > ... etc ...
      > >
      > > I expected the output to range from 0 to 255, but I get values from
      > > -128 to 127!!? and if I use (unsigned int) instead of (int) the output
      > > is even worst.
      > >
      > > Is there a better way to see what is the integer equivalent that each
      > > byte holds ?
      > >
      > > Regards,
      > > Alexis
      > > _______________ ________
      > > bostonmegarocke r@yahoo.com[/color][/color]

      Comment

      Working...