Productivity and Utilities

LZW Compression Stepper

Encode text or decode codes and watch the LZW dictionary build, one step at a time.

Your numbers

What to do

Example text. It is read as UTF-8 bytes, up to 2000 bytes.

Result

Codes sent

84 79 66 69 79 82 78 79 84 256 258 260 265 259 261 263

24 bytes became 16 codes.

Input bytes
24
Codes
16
Bits if each code uses 12 bits
192
Bits as plain bytes
192
Ratio of the two
100.0%
Dictionary entries at the end
271

Steps

StepPrefixNextCode sentNew entry
1TO84256 = TO
2OB79257 = OB
3BE66258 = BE
4EO69259 = EO
5OR79260 = OR
6RN82261 = RN
7NO78262 = NO
8OT79263 = OT
9TT84264 = TT
10TOB256265 = TOB
11BEO258266 = BEO
12ORT260267 = ORT
13TOBE265268 = TOBE
14EOR259269 = EOR
15RNO261270 = RNO
16OTend263
How this was calculated
  1. The dictionary starts with one entry for every byte, codes 0 to 255. The coder keeps the longest prefix that is already in the dictionary and reads one more byte.
  2. When prefix plus the new byte is not in the dictionary, the code of the prefix is sent and prefix plus byte is added as the next entry (256, 257, and so on). The new byte starts the next prefix.
  3. When the dictionary reaches 4096 entries (12-bit codes) no more entries are added. A space is drawn as ␣. The 12-bit figure is an example width; real formats such as GIF and TIFF grow the width as the dictionary fills.

Calculated using SumPanda.com · https://sumpanda.com/lzw-compression-stepper/

Results are estimates for general information. Verify important figures before relying on them.

Next step

How to use the lzw compression stepper

  1. Choose Encode text to compress, or Decode codes to expand a list of numbers.
  2. To encode, enter the text. It is read as UTF-8 bytes and up to 2,000 bytes are accepted.
  3. To decode, enter numbers from 0 to 4095 separated by spaces or commas.
  4. Read the codes sent, the size comparison and the step table showing the prefix, the next byte, the code sent and the new dictionary entry.

Formula

start with entries 0 to 255, one for each byte

keep the longest known prefix w; read byte c

if w + c is known, w = w + c; otherwise send the code of w, add w + c as the next entry and set w = c

The decoder rebuilds the same dictionary from the codes. A code may name the entry the encoder added just before sending it; that entry is the previous text plus its own first byte.

TOBEORNOTTOBEORTOBEORNOT

The 24 bytes become 16 codes: 84 79 66 69 79 82 78 79 84 256 258 260 265 259 261 263. The first nine codes are single letters, and the later ones name phrases built earlier, such as TO (256), BE (258), OR (260) and BEO (265).

At 12 bits per code the output needs 192 bits, exactly the same as the 24 bytes x 8 = 192 bits of the input, so this short text does not shrink at all. Longer text with repeats is where the method pays off. The input aaaaaaa gives 97 256 257 97.

Where LZW is used

Lempel-Ziv-Welch coding appears in the GIF and TIFF image formats and in older compression tools. It needs no table sent along with the data, because the decoder builds the same table. That is its main advantage over the code tables of the Huffman Coding Calculator.

Real formats vary the code width as the dictionary grows, and have special codes to clear the table. This page uses a fixed 12 bit width as an example and stops adding entries when the dictionary reaches 4,096. For other ways of writing binary data as text, see the Base64 Encoder and Decoder.

The repeated string case

For aaaaaaa the encoder sends 97, then 256 for aa, then 257 for aaa. When the decoder reads 256 it has not yet built entry 256, but it knows the entry must be a followed by the first letter of itself, which is aa. The decoding test, 97 256 257 258 giving ten a letters, covers exactly this case.

Assumptions and limits

  • Input text is converted to UTF-8 bytes, so accented letters and emoji take more than one byte.
  • A space is drawn as a visible symbol in the table, and unreadable bytes as \x and two hex digits.
  • The size comparison assumes 12 bits for every code, an example width.
  • Only the first 60 steps are shown; the codes list is complete.
  • Decoding stops with a message if a code is not in the dictionary yet or the first code is not a single byte.

Formulas reviewed October 10, 2026. See the calculation methodology for how SumPanda rounds, tests and sources its formulas.

Frequently asked questions

Why did my short text not get smaller?

Each code takes 12 bits in the comparison, more than the 8 bits of a plain byte. A text has to repeat enough for the longer phrases to repay that.

Why does decoding sometimes use a code I have not seen?

The encoder adds an entry and may use it at once. The decoder builds it one step later, so it works the entry out from the previous text.

Is LZW the same as ZIP?

No. ZIP files usually use a different method, called Deflate. LZW is an older relative from the same family.

Can I decode codes from a GIF file?

Not directly. GIF packs variable width codes and clear codes into blocks, which this page does not read.