I\'m working on a Huffman coding/decoding project in C and have a good understanding of how the algorithm should store information about the Huffman tree, re-build the tree duri
Here's some pseudo-code to give you the general idea:
static byte BitBuffer = 0;
static byte BitsinBuffer = 0;
static void WriteBitCharToOutput(char bitChar);
// buffer one binary digit ('1' or '0')
{
if (BitsInBuffer > 7)
{
stream.write(BitBuffer);
BitsInBuffer = 0;
BitBuffer = 0; // just to be tidy
}
BitBuffer = (BitBuffer << 1) | (bitChar == '1' ? 1 : 0);
BitsInBuffer++;
}
static void FlushBitBuffer()
// call after last character has been encoded
// to flush out remaining bits
{
if (BitsInBuffer > 0)
do
{
WriteBitCharToOutput('0'); // pad with zeroes
} while (BitsInBuffer != 1);
}