Link to home
Start Free TrialLog in
Avatar of LiamV
LiamV

asked on

Reading Hex values - how to use 'Unsigned Byte' value instead of signed byte value

Hi there, ive got some code which reads 4 bytes of the Hex values of a file (specified by a command line argument), and prints them out. For some reason, when I read in a value which has a negative signed byte value, the program outputs a small number of F's before displaying the actual hex value. Could anyone help me on this one? The code is here:

---------------------------------------
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <iomanip>

#define NO_BYTES 4

char readbuffer[NO_BYTES];
int no_files;
int i;

using namespace std;

int main(int argc, char *argv[])
{
  ifstream InFile;
  InFile.open(argv[1], ios::in | ios::binary);  

  //print filename to file
  printf("File Name: ");
  printf(argv[1]); //print first argument (filename)
 
  //print filesize to file
  InFile.getline(readbuffer, NO_BYTES+1);
  cout << endl << "Header: ";
 
  //print hex values  
  while (i<NO_BYTES)
  {
  printf("%02X",readbuffer[i]);
  i++;
  }
  cout << endl;
   
  system("PAUSE");      
  return 0;
}
-------------------------------
Avatar of imladris
imladris
Flag of Canada image

The value for -2 in hex in 4 bytes is 0xfffffffe. So one would expect to print those f's with a %0X format specifier. What do you expect it to print?
>> char readbuffer[NO_BYTES];

should probably be,

unsigned char readbuffer[NO_BYTES];

because you then have a sign biy for every byte in the 4 byte sequence.

Exceter
It seems apparent that your input file does not contain these values as hex text or you would simly display the contents of the array. Therefore, if it is binary, I think you should be using read instead of getline. For example,

InFile.read((char*)readbuffer, NO_BYTES);

You don't need to null terminate the array unless youplan to use this array in functions that depend on this. Since the array contains binary data, this isn't very likely but I thought I'd mention it anyway.

Exceter
Avatar of LiamV
LiamV

ASKER

thankyou, but neither works, using an unsigned char with getline or read gives an invalid converstion error (unsigned char* -> char *). Basicly if the input file has the hex string 01D7, the program will output the 01 byte, then will output 'FFFFFFD7000' for the second byte, instead of just 'D7' which i want.
>> Basicly if the input file has the hex string 01D7

If the input file contains a hex string, then you don't need to do any conversions at all. For example,

std::string number;
ifstream in( argv[1], ios::in );

in >> number;
cout << number << endl;

in.close();

-- input file --
01D7
-- eof--

-- output --
01D7

Exceter
Avatar of LiamV

ASKER

The hex values are not in a text file. They are in a binary file and i need to get the hex values for the bytes.
ASKER CERTIFIED SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Cross posted there. So, the casting should help then.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LiamV

ASKER

Thankyou both for the comments. imladris, your code worked fine and removed all those FF's from the output. Exceter im sorry but your code seems to take bytes from further on in the steam and output them in a peculiar order.
>> Thankyou both for the comments.

You are most welcome.

>> Exceter im sorry but your code seems to take bytes from further on in the steam...

It would if you only read in two bytes.

>> ...and output them in a peculiar order.

That sounds like an endian issue. In Big Endian binary numbers are stored with the high order bytes on the left while Little Endian stores the high order bytes on the right. For example, in Big Endian, the following value would be written to disk as,

Value: 00FF23AB
Written: 00FF23AB

In little Endian it would be written as,

Value: 00FF23AB
Written: AB23FF00

Cheers!
Exceter