Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

C++ assigning bit values.


ghost's Avatar
0 0

I'm trying to create a simple compression program that will take the most frequently appearing character and assign it a value of 0, the second most frequent 1, third 10 and so forth. This will ultimately result in a smaller file. The two questions I have are: How do I create a file that doesn't contain more characters (the third character being represented by 10 would result in a much larger file)? How would I tell the compiler to store the characters as the exact bits I want?


ghost's Avatar
0 0

Sqwertle wrote: I'm trying to create a simple compression program that will take the most frequently appearing character and assign it a value of 0, the second most frequent 1, third 10 and so forth. This will ultimately result in a smaller file. The two questions I have are: How do I create a file that doesn't contain more characters (the third character being represented by 10 would result in a much larger file)? How would I tell the compiler to store the characters as the exact bits I want? It's early, but I'll take a stab at this one.

Generally, you're storing ASCII characters in a text file (forget about Unicode for a minute). ASCII is a character set made up of 128 regular character codes and 128 extended character codes that relate to letters or symbols. That's 1 byte (128 or 2^7) and 1 byte (128 or 2^7), but you only use a single byte to represent normal text.

Thus to save your binary representations to a file, you do not write the actual binary to the file but, rather, write the ASCII character code for the decimal equivalent of the binary representation.

Then, when you're reading it in, you can get the ordinal of the ASCII character and convert that back to binary. When you're reading it in C++, you might want to read the file back in using binary I/O for greater precision (just adding an ios::binary in the arguments, I think).