Toolstia / Huffman coding calculator

Huffman coding calculator

Huffman coding gives short bit codes to common symbols and long codes to rare ones. Enter some text to see the code for each symbol, the encoded bit string, how many bits you save against plain 8 bit characters, and the entropy limit.

Huffman coding gives short codes to common symbols and long codes to rare ones, so that no code is the start of another. Ties are broken by which symbol appears first, so the codes you get may differ from another tool, but the total size is the same.

How it works

  1. Type or paste your text.
  2. Read the code table and the totals.
  3. Copy the encoded bits if you need them.

How it works

The two least common symbols are joined into one node again and again until a single tree is left. Reading left as 0 and right as 1 gives each symbol its code.

Prefix free

No code is the beginning of another code, so the bit string can be decoded without any separators.

Entropy

Entropy is the lowest average number of bits per symbol that any code can reach. Huffman coding gets within one bit of it.

Ties

When counts tie, the symbol that appears first in the text goes first. Another tool may choose differently, but the total number of bits is the same.

Questions

Is my text uploaded?

Everything happens in your browser, and nothing you enter is sent anywhere.

Why is the compression small for short text?

A real file also needs to store the code table. This tool only counts the encoded bits.

What if there is only one different symbol?

It gets the one bit code 0, so the text takes one bit per character.