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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cliff_mAuthor Commented:
Edited text of question.
cliff_mAuthor Commented:
Edited text of question.
cliff_mAuthor Commented:
Edited text of question.
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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;
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.