Write binary file - 8bit words

Hello,

I am having trouble writing a binary file. The procedure I am using is:

1
2
3
4
5
6
7
8
9
StreamReader^ sr = gcnew StreamReader( InputFileName );
FileStream^ fs = gcnew FileStream("data.bin", FileMode::Create);
BinaryWriter^ sw = gcnew BinaryWriter(fs);
String^ line;

while ( line = sr->ReadLine() )
{
sw->Write(line[0]);
}


The file I am reading is a text file which has characters 0-9, a-f. So they are basically hex.
I want to write these characters to a binary file. And it should be 8-bits in length. When I write now with the following method, it writes 32-bits. So I get 24 0's.

Is there a way I can write 8-bits, or 4-bits. Because all are hex, they will only have 4-bits in length.

Thank you very much.
I'm not sure about 4 bits but surely if you used a variable which is 8 bits long such as a char as opposed to a string character you could write 8 bits as binary.
maybe adding add something similar to this after line 8 (replacing line 8 also)
char charOut = line[0];
sw->charOut;

Not 100% sure on that though, might work
Topic archived. No new replies allowed.