|
Prev: range of an int
Next: Basic inoput issue
From: Ron on 13 Jan 2008 20:40 I am trying to write hex bytes to a binary file. I have no problem doing this if I use hard coded values like this; #include <fstream.h> ofstream OutputFileStream ; OutputFileStream.open("binaryfile.bin", ios::binary) ; OutputFileStream << '\xF6' ; OutputFileStream.close() ; But what I really want to do is the same thing using variables. And this is where I can't get it to work. If I use this; #include <fstream.h> int intF7 = 0xF7 ; ofstream OutputFileStream ; OutputFileStream.open("binaryfile.bin", ios::binary) ; OutputFileStream << hex << intF7 ; OutputFileStream.close() ; then the characters 'F' and '7' get written to the file. And if I use this method; #include <fstream.h> #include <iomanip.h> int intF7 = 0xF7 ; ofstream OutputFileStream ; OutputFileStream << setiosflags(ios::hex) ; OutputFileStream.open("binaryfile.bin", ios::binary) ; OutputFileStream << intF7 ; OutputFileStream.close() ; this also writes the characters 'F' and '7' to the file. How do I write the hex bytes as hex to the binary file using variables that hold the hex data?
From: Daniel T. on 13 Jan 2008 21:16 "Ron" <ron1957(a)optusnet.com.au> wrote: > I am trying to write hex bytes to a binary file. > I have no problem doing this if I use hard coded values like this; > > #include <fstream.h> > ofstream OutputFileStream ; > OutputFileStream.open("binaryfile.bin", ios::binary) ; > OutputFileStream << '\xF6' ; > OutputFileStream.close() ; > > But what I really want to do is the same thing using variables. > And this is where I can't get it to work. > If I use this; > #include <fstream.h> > int intF7 = 0xF7 ; > ofstream OutputFileStream ; > OutputFileStream.open("binaryfile.bin", ios::binary) ; > OutputFileStream << hex << intF7 ; > OutputFileStream.close() ; > > then the characters 'F' and '7' get written to the file. > > And if I use this method; > #include <fstream.h> > #include <iomanip.h> > int intF7 = 0xF7 ; > ofstream OutputFileStream ; > OutputFileStream << setiosflags(ios::hex) ; > OutputFileStream.open("binaryfile.bin", ios::binary) ; > OutputFileStream << intF7 ; > OutputFileStream.close() ; > this also writes the characters 'F' and '7' to the file. > > How do I write the hex bytes as hex to the binary file using variables that > hold the hex data? Use the unformatted output functions. basic_ostream::put and basic_ostream::write.
From: Alf P. Steinbach on 13 Jan 2008 21:18 * Ron: > I am trying to write hex bytes to a binary file. There's no such thing as hex bytes. A byte is a sequence of bits. The bitpattern (sequence of bit values) can be designated in hexademical notation, in decimal notation, or as roman numerals, whatever. > I have no problem doing this if I use hard coded values like this; > > #include <fstream.h> Note: there's no such thing as <fstream.h> in standard C++. Use #include <fstream> > ofstream OutputFileStream ; > OutputFileStream.open("binaryfile.bin", ios::binary) ; > OutputFileStream << '\xF6' ; > OutputFileStream.close() ; '\xF6' denotes char value 0xF6. I'm not sure exactly what is guaranteed about the resulting value when that's outside the value range of char... Ah, OK, the Holy Standard comes to the rescue, �2.13.2/4: "The value of a character literal is implementation-defined if it falls outside of the implementation-defined range for char (for ordinary literals) or wchar_t (for wide literals)". Note that with signed char, 0xF6 is outside the implementation-defined range on most implementations, i.e., the value is implementation-defined. Using unsigned char, operator<< would most probably treat it as an integer and convert to a sequence of chars, as decimal representation. So, without relying on implementation-defined behavior, unsigned char is necessary but not sufficient. In addition, you'll need to use write() instead of operator<<; write just dumps the data, whereas operator<< performs a conversion to text. > But what I really want to do is the same thing using variables. > And this is where I can't get it to work. > If I use this; > #include <fstream.h> > int intF7 = 0xF7 ; > ofstream OutputFileStream ; > OutputFileStream.open("binaryfile.bin", ios::binary) ; > OutputFileStream << hex << intF7 ; > OutputFileStream.close() ; > > then the characters 'F' and '7' get written to the file. As you requested, a conversion to text. > And if I use this method; > #include <fstream.h> > #include <iomanip.h> > int intF7 = 0xF7 ; > ofstream OutputFileStream ; > OutputFileStream << setiosflags(ios::hex) ; > OutputFileStream.open("binaryfile.bin", ios::binary) ; > OutputFileStream << intF7 ; > OutputFileStream.close() ; > this also writes the characters 'F' and '7' to the file. > > How do I write the hex bytes as hex to the binary file using variables that > hold the hex data? See above. You're requesting conversion to text by using operator<<. Cheers, & hth., - Alf -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail?
From: Jim Langston on 14 Jan 2008 20:18 In addition to the other good comments you received. Ron wrote: > I am trying to write hex bytes to a binary file. > I have no problem doing this if I use hard coded values like this; > > #include <fstream.h> > ofstream OutputFileStream ; > OutputFileStream.open("binaryfile.bin", ios::binary) ; > OutputFileStream << '\xF6' ; '\xF6' is a character. Characters get written as characters. > OutputFileStream.close() ; > > But what I really want to do is the same thing using variables. > And this is where I can't get it to work. > If I use this; > #include <fstream.h> > int intF7 = 0xF7 ; Hmm.. this is an int. And since operator<< How would you expect operator<< to treat an interger value? It may not fit in a character, and it doesn't look like you're working with binary data when you deal with ints, the default is generally to translate it to an ASCII string for the value. You should do what the others said, but you might just want to experiment and change that line to: char F7 = 0xF7; and see if you get your expected out put. Note that you'll still run into trouble with some special values (such as 0x0D and 0x0A which will probably get translated somehow). > ofstream OutputFileStream ; > OutputFileStream.open("binaryfile.bin", ios::binary) ; > OutputFileStream << hex << intF7 ; > OutputFileStream.close() ; > > then the characters 'F' and '7' get written to the file. > > And if I use this method; > #include <fstream.h> > #include <iomanip.h> > int intF7 = 0xF7 ; > ofstream OutputFileStream ; > OutputFileStream << setiosflags(ios::hex) ; > OutputFileStream.open("binaryfile.bin", ios::binary) ; > OutputFileStream << intF7 ; > OutputFileStream.close() ; > this also writes the characters 'F' and '7' to the file. > > How do I write the hex bytes as hex to the binary file using > variables that hold the hex data? I just told you why the output is as you are getting (translation to an ASCII string) not really how to fix it. To fix it you need to follow the other posts. operator<< is generally not a good idea for binary data. -- Jim Langston tazmaster(a)rocketmail.com
From: Ron on 18 Jan 2008 00:48
Thanks to all. I'm getting the result I want now by using the fstream write() function like this; int buff[1]; buff[0] = 247; OutputFileStream.open("binaryfile.bin", ios::binary) ; OutputFileStream.write((char*)buff, 1); Of course this is building up to eventually writing a very long stream of binary data to a file but I needed to resolve this issue first. "Jim Langston" <tazmaster(a)rocketmail.com> wrote in message news:4STij.69$6R5.58(a)newsfe05.lga... > In addition to the other good comments you received. > > Ron wrote: > > I am trying to write hex bytes to a binary file. > > I have no problem doing this if I use hard coded values like this; > > > > #include <fstream.h> > > ofstream OutputFileStream ; > > OutputFileStream.open("binaryfile.bin", ios::binary) ; > > OutputFileStream << '\xF6' ; > > '\xF6' is a character. Characters get written as characters. > > > OutputFileStream.close() ; > > > > But what I really want to do is the same thing using variables. > > And this is where I can't get it to work. > > If I use this; > > #include <fstream.h> > > int intF7 = 0xF7 ; > > Hmm.. this is an int. And since operator<< How would you expect operator<< > to treat an interger value? It may not fit in a character, and it doesn't > look like you're working with binary data when you deal with ints, the > default is generally to translate it to an ASCII string for the value. > > You should do what the others said, but you might just want to experiment > and change that line to: > > char F7 = 0xF7; > and see if you get your expected out put. Note that you'll still run into > trouble with some special values (such as 0x0D and 0x0A which will probably > get translated somehow). > > > ofstream OutputFileStream ; > > OutputFileStream.open("binaryfile.bin", ios::binary) ; > > OutputFileStream << hex << intF7 ; > > OutputFileStream.close() ; > > > > then the characters 'F' and '7' get written to the file. > > > > And if I use this method; > > #include <fstream.h> > > #include <iomanip.h> > > int intF7 = 0xF7 ; > > ofstream OutputFileStream ; > > OutputFileStream << setiosflags(ios::hex) ; > > OutputFileStream.open("binaryfile.bin", ios::binary) ; > > OutputFileStream << intF7 ; > > OutputFileStream.close() ; > > this also writes the characters 'F' and '7' to the file. > > > > How do I write the hex bytes as hex to the binary file using > > variables that hold the hex data? > > I just told you why the output is as you are getting (translation to an > ASCII string) not really how to fix it. To fix it you need to follow the > other posts. operator<< is generally not a good idea for binary data. > > -- > Jim Langston > tazmaster(a)rocketmail.com > > |