C++ io-stream how to write a real hex file

  • Thread starter Markus Hämmerli
  • Start date
M

Markus Hämmerli

I ll try to write a file with a iostram. It is ok for ASCII but don't work
for hex .

So when i send a "01" I get ASCII 31 but Iwant a real 01 in the file.



Thanks Markus (e-mail address removed)

This ist my code so fare:

std::eek:fstream fileout(_T("C:\\COMbin.hex"),std::ios::eek:ut
|std::ios::trunc|std::ios::binary);

// fileout.setf(std::ios::showbase |std::ios::hex);

int i=0x01;

fileout<< i;
 
J

John Harrison

Markus Hämmerli said:
I ll try to write a file with a iostram. It is ok for ASCII but don't work
for hex .

So when i send a "01" I get ASCII 31 but Iwant a real 01 in the file.



Thanks Markus (e-mail address removed)

This ist my code so fare:

std::eek:fstream fileout(_T("C:\\COMbin.hex"),std::ios::eek:ut
|std::ios::trunc|std::ios::binary);

// fileout.setf(std::ios::showbase |std::ios::hex);

int i=0x01;

fileout<< i;

Use write for binary output

fileout.write((char*)&i, sizeof i);

Also use read for binary input. << is for text only.

john
 
R

Rolf Magnus

Markus said:
I ll try to write a file with a iostram. It is ok for ASCII but don't
work for hex .

Well, operator<< is for formatted text output. So it will convert
everything you have to text and write that text.
If you want to write binary data (I assume that's what you mean by
"hex"), you should read the faq at
http://www.parashift.com/c++-faq-lite/
Look especially at the chapter about serialization.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top