Hey guys,
I wanted some clarification in my understanding of bits and bytes in C. For practice, I'm writing an LZW compression program. The idea is that I will get input from stdin, encode it, output the codes, and then decode them. So the encoded file will be something like:
ABC256257 for input ABCABBC...
And my dictionary is: 0-255 for the 256 ASCII characters, 256 = AB, 257 = BC, 258 = CA, 259 = ABB, etc.
The issue is that in 8 bits, I can only represent numbers from 0 to 255, so if I want to output "256" as a single byte, I will need 9 bits. I found a program that has gets and puts bits to stdout, but I'm not sure how to use it correctly. The two methods are:
put(int code, int bits), which puts a code of size bits into stdout. get(int bits) will get the code of size bits.
So I figured that put(256, 9) should put the number 256 into stdout, but all it does is put some weird symbols like €"Á °("
My questions are:
1. Do I have the right idea? should I be doing put(256, 9) or something else?
2. If so, then why does that print weird symbols.
Thanks for your help.