Some guidance needed

Discussion in 'C++' started by bostonmegarocker, Jun 30, 2003.

  1. 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(charArray, size);

    for(int= 0; i<size; i++)
    {
    outFile << (int)charArray <<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
    _______________________
     
    bostonmegarocker, Jun 30, 2003
    #1
    1. Advertising

  2. bostonmegarocker

    Min Guest

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

    "bostonmegarocker" <> wrote in message
    news:...
    > 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(charArray, size);
    >
    > for(int= 0; i<size; i++)
    > {
    > outFile << (int)charArray <<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
    > _______________________
    >
     
    Min, Jun 30, 2003
    #2
    1. Advertising

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

    "Min" <> wrote in message news:<fY0Ma.327717$>...
    > I assumed that 0-255 means, you don't have negative value.
    > Then try "unsigned char"
    >
    > "bostonmegarocker" <> wrote in message
    > news:...
    > > 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(charArray, size);
    > >
    > > for(int= 0; i<size; i++)
    > > {
    > > outFile << (int)charArray <<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
    > > _______________________
    > >
     
    bostonmegarocker, Jul 1, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jm
    Replies:
    3
    Views:
    386
    Kevin Spencer
    Jan 21, 2004
  2. new
    Replies:
    5
    Views:
    434
  3. Alan Isaac
    Replies:
    3
    Views:
    292
    Alan Isaac
    Jun 28, 2007
  4. Penn
    Replies:
    5
    Views:
    577
  5. Greg Woods
    Replies:
    2
    Views:
    1,153
    Peter Bucher [MVP]
    Apr 1, 2008
Loading...

Share This Page