reading 1, 2 and 4 bytes at a time and displaying Hex

I am trying to read in parts of a proprietary file, which has header with useful information. I would like to take the header information and print it out in human readable form to an XML file. I have no problem opening and printing to files, my problem is in reading the data. Everything I see seems to be geared toward text, which would no work in my case.

I have two samples, one in perl, the other my current c attempt. The one in Perl is exactly what I want to do.

1. Perl
$file = shift(@ARGV) ;
open(FH, $file) ;
read FH, $buf, 2 ;
$buf = unpack('H*', $buf) ;
print "<Version>$buf</Version>\n" ;

2. C(++)
Custfile::ReadHeader(const char *filename) {
            ifstream inFile( filename, ios::nocreate) ;
            if ( !inFile ) {
                  cerr << "File could not be opened\n" ;
                  return 1 ;

            short version ;
            inFile >> version ;
            cout << "<Version>" << version << "</Version>" << endl ;
            return 0 ;

I have to read other sizes, and other subsequent variables after this one, but I am hoping once I know how to do it once, I can apply the method.

The first 16 bytes I am dealing with is(in hex):
02 00 46 46 19 02 90 01 9F 37 23 00 00 00 00 00

The intermediate result that I am hoping for is to have <Version>0200</Version> printed to stdout.

Who is Participating?
nietodConnect With a Mentor Commented:
You are used to reading ASCII data.  This is binary data.  (You already knew that, though.)

To read the binary data us the istream's read() function to read directly into a variable of the cooresponding type.

Example follows.
cliff_mAuthor Commented:
Edited text of question.
cliff_mAuthor Commented:
Edited text of question.
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

cliff_mAuthor Commented:
Edited text of question.
Assuming that on your compiler an int is 4 bytes and a short int is 2 bytes.  And assuming for the sake of an example that the file stores a 4 byte integer, then a 2 byte integer, then a 4 byte integer, you could do

int Int1;
short int Int2;
int Int3;,4);,2);,4);

Let me know if you have any questions.
cliff_mAuthor Commented:
I have looked at using read, I just have not figured out how to display the result '0200', after reading in 2,4 or however many bytes. My guess is there is a hex function, I will look this up when I get to work. Good answer, you will get the credit (points).

You can probably tell, I have had some C and C++ classes, but my heart belongs to Perl. :)
I see.  

Reading and writting are totally independant.  i.e. you can write an integer value in any format, it doesn't matter the format you read it in.  For example you could read the integer in ASCII decimal, like "1234" and then write it in ASCI Hex, like ""04D2"  okay?

You are reading the balues in binary, because that is how they are stored in the files, that is what I showed you.  Once read, you can ouput the values in any way you want, it doesn't matter how they werw read

I think you want to output the values in ASCII Hex, that is, as an ASCII representation of the value expressed in Hex, is that correct?

To do that you would use the Hex I/O manipulator, like

OutFile << hex << TheInt;

If you need to set the width of the value ouput or other formatting issues, you would do that with typical I/O manipulatiors, like "setw".  i.r

OutFile << setw(4) << hex << TheInt;
All Courses

From novice to tech pro — start learning today.