Link to home
Start Free TrialLog in
Avatar of sandeep_th
sandeep_th

asked on

Write a program that will display the contents of a file using hex digits and create a text file out of it

Hi Experts,
I have a binary file. I want to read this file and give a hex output(as text) and store it in another file.

To make it clear, lemme give an example....

supposing there is a binary file...and on opening in Ultraedit(in hex mode) .....one sees an output like this:-

00000000 : 01 10 89 00 00 E1 A5 04 10 13 00 00 89 00 00 00 ................
00000010 : 01 00 00 00 83 00 00 00 02 10 04 10 13 00 00 0C ................

and so on

where the first column is the number of bytes read(in hex), the values in the middle are the hex values for the binary data, and the dots on the right signify the binary data.....each dot for one byte.

What I want is that the binary file be read and an ascii file be created which when opened reads like:-

01 10 89 00 00 E1 A5 04 10 13 00 00 89 00 00 00
01 00 00 00 83 00 00 00 02 10 04 10 13 00 00 0C


I know this question has been asked many times in this forum. But somehow I didn't get exactly what I wanted.

The closest that came to the answer is this one:-

https://www.experts-exchange.com/questions/10328683/i-need-to-open-a-file-in-hex-and-save-all-the-data-to-a-txt-file.html?query=hex+binary+file+&searchType=topic

But the output file it creates doesn't have the right values(the output is rather weird in fact....every value is preceeded by an 8)

C++( and NOT C) answers only please


ASKER CERTIFIED SOLUTION
Avatar of efn
efn

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 sandeep_th
sandeep_th

ASKER

Works like a charm!!!! Thanks!!

Just one more thing.....I'm using the following line is used to write into the file:

Out <<setfill('0')<<setw(2)<< hex << Chr;

Supposing instead of writing the formatted hex value to the output stream, I want to write it into a string variable(for comparisons etc.).....how do we do that?

Use an ostringstream.  You can write to it as you write to a stream, and then you can retrieve the string with the str() member function.

http://www.cplusplus.com/ref/iostream/ostringstream/

--efn