Hi guys..
In the huffman algorithm I have made an output file contain 0 and 1..
how can I say to compiler that these 1 and zero are bit no Byte.
and how can i make the real Huffman output that its size is lesser than the initial file,
What is the huffman output?
tnx.

Dani AI

Generated

Short, practical follow-ups to the thread (ties to and ):

You must pack Huffman bits into bytes, record enough metadata for the decoder, and be consistent about bit order. was right that streams operate in bytes; 's change to the shift direction shows the key point: whatever ordering you pick (MSB-first or LSB-first) the decoder must use the same ordering. Also record how many bits in the final output byte are real (padding) so the last byte can be trimmed on decode.

A compact bit-buffer pattern (MSB-first) for writing bits to a binary file:

unsigned bit_acc = 0;
int bit_count = 0;

void push_bit(std::ofstream &out, int bit) {
    bit_acc = (bit_acc << 1) | (bit & 1);   // add new bit at LSB
    if (++bit_count == 8) {
        unsigned char b = static_cast<unsigned char>(bit_acc);
        out.put(static_cast<char>(b));
        bit_acc = bit_count = 0;
    }
}

void flush_bits(std::ofstream &out) {
    if (bit_count) {
        bit_acc <<= (8 - bit_count); // pad low bits with zeros
        out.put(static_cast<char>(static_cast<unsigned char>(bit_acc)));
        // write '8 - bit_count' into header or immediately after header
    }
}

Header and portability notes:

  • Minimal header must include a magic/signature, a representation of the tree or code lengths, and the number of padding bits in the final byte. Options for the tree: full frequency table, serialized tree (preorder), or canonical Huffman code (saves header size).
  • Always open files in binary mode when writing compressed bytes so the runtime will not translate newlines. On Windows text mode translates CR/LF; in binary you will see 13 (CR) and 10 (LF) bytes directly. Use unsigned char for raw bytes to avoid sign-extension when manipulating values.
  • If you prefer a smaller header, store canonical code lengths and rebuild the canonical codes at decode time; see Huffman coding and Canonical Huffman code. For binary open flags and write/read functions see the standard reference: ios::openmode flags.

Common troubleshooting: open both input and output in binary, check that encoder/decoder agree on MSB/LSB ordering, write the padding count into the file header (or patch it after you finish writing), and test round-trip on small files first so you can inspect raw bytes.

Recommended Answers

All 6 Replies

Hi guys..
In the huffman algorithm I have made an output file contain 0 and 1..
how can I say to compiler that these 1 and zero are bit no Byte.
and how can i make the real Huffman output that its size is lesser than the initial file,
What is the huffman output?
tnx.

You can't output a single bit. The smallest amount of data that can be written to any storage ( register, cache, ram, HD ) is a byte. If you want to write individual bits, you need to first package them in bytes. So, take your 8 bits, use the left shift operator ( << ) to move the bits to the correct offest, then use a bitwise or ( || ) to combine them into a single byte.

char bits[8] = { 1, 1, 0, 0, 1, 1, 0, 1 };
char byte = 0;
for( int i=0; i<8; i++ )
    byte = byte | ( bits[i] << i );

Don't sweat the "char" type. It's simply an 8 bit integer. Note that the order of bits in the array is "graphically" reversed, so the least significant bit is actually the first item in the array. So, the byte will actually equal the binary value 10110011.

Good luck

Tnx so much for your help,if we change the code in this way :
byte = byte | ( bits << 7-i );
then we protect the order of byte :d

Tnx so much for your help,if we change the code in this way :
byte = byte | ( bits << 7-i );
then we protect the order of byte :d

It depends on byte-ordering. Most modern architectures use a little-endian byte ordering, which means that the least significant bit is on the far right.

1 0 0 1 1 0 1
            ^
            |
           LSB

However, in arrays, we usually think of the element with the lowest order as being on the far left ( at offset 0 )

[1, 0, 1, 1, 0, 0, 1]
 ^
 |
 Lowest Order Element

You should think long and hard about what convention makes the most sense to you. There is no convention that is truly more correct ( though many would fiercely debate that ), however, you should make sure that you use a consistent convention to avoid confusion.

I have another problem in implemention of Huffman code..
in some file there are chararacters that stop the reading of the chars from file if we put flag on default and if we put flag on binary we face a new problem.. in this case for example "Enter" read by get() translate '13' instead of 10 ... what can i do to solve this problem...
tnx

I have another problem in implemention of Huffman code..
in some file there are chararacters that stop the reading of the chars from file if we put flag on default and if we put flag on binary we face a new problem.. in this case for example "Enter" read by get() translate '13' instead of 10 ... what can i do to solve this problem...
tnx

Well, I'm not sure exactly what you are asking here. If you are confused about processing binary files, this website might help you out ( just scroll down to the binary files section). If you post the code that processes the file and its exact output, that would help.

Well, I'm not sure exactly what you are asking here. If you are confused about processing binary files, this website might help you out ( just scroll down to the binary files section). If you post the code that processes the file and its exact output, that would help.

I have read it before!
tnx,,, my problem solved! ! !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.