We help IT Professionals succeed at work.

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

sandeep_th
sandeep_th asked
on
193 Views
Last Modified: 2010-04-02
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/Programming/Programming_Languages/Cplusplus/Q_10328683.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


Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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?

efn

Commented:
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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.