Solved

i need to  open a file in hex and save all the data to a txt file

Posted on 2000-04-14
4
230 Views
Last Modified: 2010-04-02
i need to  open a file in hex and save all the data to a txt file
0
Comment
Question by:ProgramDesign
  • 2
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 2718108
Read the binary file as a byte stream. Use the sprintf function to convert each byte to a hex string. Write the strings to the text file.

unsigned char byte;
//...

char buf[8];
sprintf(buf, "%02X ", (unsinged int)byte);
0
 
LVL 22

Expert Comment

by:nietod
ID: 2718310
That is a very C way to do it.  It works, but I'm surprised chensu wasn't more C++ ish.

for a more C++ approach you can open the file with an fstream object and read bytes.  Then ouput the bytes to another fstream object using the hex  i/o manipulator, like

ifstream In("C:\\SOMEFILE.DAT",ios::in | ios::binary);
ofstream Out("C:\\SOMEFILE.TXT",ios::out);

while (true)
{
   unsigned char Chr;
   In  >> Chr;
   if (In.eof()) // If at the end of the file.
      break; // Stop the loop.
   Out << ios::hex << Chr;
};


Depending on what you want the hex to look like, you might want to add spaces and or/controll the width of the hex numbers pritned.
0
 
LVL 23

Expert Comment

by:chensu
ID: 2718554
>but I'm surprised chensu wasn't more C++ ish.

Right. Honestly, I have been doing Windows GUI programming, so I seldom use those stuff, such as cin, cout. The first idea I came up with immediately was to use the Windows API function wsprintf.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2718806
You have to write "I will not use C in C++" 100 times..... :-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 141
Exception thrown at 0x00007FFD5BC81F28 7 49
Unable to start eclipse ? 17 152
How do I get Window Title of all opened process? 4 11
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question