Solved

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

Posted on 2000-04-14
4
228 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
Comment Utility
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
Comment Utility
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
Comment Utility
>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
Comment Utility
You have to write "I will not use C in C++" 100 times..... :-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now