Writing Binary

Soldato
Joined
22 Oct 2005
Posts
2,884
Location
Moving...
What's the easiest way to write binary data to a file? I want to write actual binary, as in 1 bit rather than write 0 or 1 as an int and actually take 8 bits which is what I've done in previous program that involve reading/writing to files. I need it to be in in C, C++ or preferably Java.

Thanks for any advice.
 
It is not usually possible to write individual bits to a file.

Instead what you need to do is compose the individual bits you wish to write into a single byte and then write out a byte at a time. Use C's bitwise operators to do the composition, and fwrite or fputc to write the byte, remembering when using fopen to open the file in binary format ("wb" as the second parameter).
That's the problem really. I'm doing this for image compression so I need to write as little data as possible to keep the file size small. It's kind of difficult to compress though if you can't write single bits. So for example I've got 255 symbols, symbol 25 appears a thousand times say, but symbol 67 only appears once, it would make sense to assign the value of 0 to symbol 25, and 11111111 to symbol 67. If i have to write the whole byte I will be writing 00000000 and 11111111 instead which is not saving any space.

I've managed to do run-length coding which works, but most other methods involve using binary output so now I'm kinda stuck. :(
 
Thanks Inquisitor. Yes i did mean 256 symbols, I just quickly made something up! The actual coding method i'm trying to use at the minute is Huffman encoding. This site is similar to my lecture notes in the way it is explained: http://www.si.umich.edu/Classes/540/Readings/Encoding - Huffman Coding.htm

See from their example they've got the following codes:
A - 100
E - 0
I - 1011
O - 11
U - 1010

E having the shortest code because it's the most frequent and I/U having the longest. So if you're saying you have to use a fixed number of bytes per symbol(4 in this case) to be able to distinguish between the symbols when reading it back in, how can Huffman coding work? (Sorry if I'm being a complete donut and this is really simple:o)
 
Back
Top Bottom