473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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


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 m.*********@solnet.ch

This ist my code so fare:

std::ofstream fileout(_T("C:\\COMbin.hex"),std::ios::out
|std::ios::trunc|std::ios::binary);

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

int i=0x01;

fileout<< i;

Jul 19 '05 #1
4 10738

"Markus Hämmerli" <m.*********@solnet.ch> wrote in message
news:3f***********************@newsspool.solnet.ch ...

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 m.*********@solnet.ch

This ist my code so fare:

std::ofstream fileout(_T("C:\\COMbin.hex"),std::ios::out
|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
Jul 19 '05 #2
Markus Hämmerli wrote:

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.
Jul 19 '05 #3
Don't use the stream operators, you will get ASCII. Use the read/write
methods.

"Markus Hämmerli" <m.*********@solnet.ch> wrote in message
news:3f***********************@newsspool.solnet.ch ...

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 m.*********@solnet.ch

This ist my code so fare:

std::ofstream fileout(_T("C:\\COMbin.hex"),std::ios::out
|std::ios::trunc|std::ios::binary);

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

int i=0x01;

fileout<< i;

Jul 19 '05 #4
Xenos wrote:
Don't use the stream operators, you will get ASCII. Use the read/write
methods.


Please don't top-post. Re-read section 5 of the FAQ for posting guidelines.

http://www.parashift.com/c++-faq-lite/

Your assumption that the implementation uses the ASCII character set is
unfounded.

-Kevin

--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Daniel Ehrenberg | last post by:
Io (www.iolanguage.com) is a new programming language that's purely object-oriented (but with prototypes), has a powerful concurrency mechanism via actors, and uses an extremely flexible syntax...
0
by: Joe Johnson | last post by:
Greetings, I just did a fresh install of Solaris 10 on Sparc. The OS ships with perl_5.8.4. When I run some of my scripts that run fine on Linux, I get this: IO::Seekable::getpos missing at...
1
by: Matthias Ludwig | last post by:
I'm trying to create a directory on the web server with a vb.net code: .... Dim dirName As String = "w:\filepath\images" If Not Directory.Exists(dirName) Then...
0
by: Ben Reese | last post by:
Using the FileSystemObject I can, at runtime find out which drive letter is asigned to the CD Rom Driv Dim Fso As New Scripting.FileSystemObjec For Each Drive As Scripting.Drive In Fso.Drive If...
4
by: Ben Reese | last post by:
Using the FileSystemObject I can, at runtime find out which drive letter is asigned to the CD Rom Drive Dim Fso As New Scripting.FileSystemObject For Each Drive As Scripting.Drive In Fso.Drives...
0
by: Ben Reese | last post by:
Using the FileSystemObject I can, at runtime find out which drive letter is asigned to the CD Rom Drive Dim Fso As New Scripting.FileSystemObject For Each Drive As Scripting.Drive In Fso.Drives...
9
by: Arpan | last post by:
If I am not wrong, System.IO is one of the seven namespaces that get imported in all ASP.NET page automatically but in the following code: <%@ Import Namespace="System.IO" %> <script...
0
by: muraley | last post by:
Hi, This client-server script does bi-directional communication. Setup i used: The server script on windows 2003 server and the client on a linux machine. Since i faced issues in getting the...
2
by: Nathan Sokalski | last post by:
I have an ASP.NET application which displays the directories & files in a specified directory on the server. I use the System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() to...
3
by: MyMarlboro | last post by:
Hi, When I try to use the IO::All module, I get the following error. My PC is Windows XP and Perl v 5.7.8 String found where operator expected at C:/Perl/site/lib/IO/All.pm line 12, near ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.