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

Hello,
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.

/Cliff
cliff_mAsked:
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.
0
 
cliff_mAuthor Commented:
Edited text of question.
0
 
cliff_mAuthor Commented:
Edited text of question.
0
Never miss a deadline with monday.com

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.
0
 
nietodCommented:
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;

InFile.read(&Int1,4);
InFile.read(&Int2,2);
InFile.read(&Int3,4);

Let me know if you have any questions.
0
 
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. :)
0
 
nietodCommented:
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;
0
All Courses

From novice to tech pro — start learning today.