Introduction
Run-Length Encoding (RLE) is one of the simplest lossless compression algorithms. It replaces consecutive repeated characters with a count and the character itself. For example, `AAAAABBBCC` becomes `5A3B2C`. This tool encodes and decodes RLE in both directions, with a compression ratio display so you can see when RLE helps and when it hurts.
What this tool does
- Encodes text using standard Run-Length Encoding, replacing runs of identical characters with count+character pairs.
- Decodes RLE-encoded text back to the original string.
- Toggles between encode and decode modes with a single click.
- Displays the compression ratio so you can see whether RLE helped or made the data larger.
- Processes all data locally in your browser with no network requests.
How this tool works
Select encode or decode mode, then type or paste text into the input field. In encode mode, the tool scans the input for runs of identical characters and replaces each run with the count followed by the character. In decode mode, it reads count+character pairs and expands them back to the original string. The compression ratio is displayed as (encoded length / original length) so you can judge effectiveness. All processing happens instantly in your browser.
How Run-Length Encoding works
Run-Length Encoding is a form of lossless data compression where sequences of the same data value are stored as a single value and a count. It was originally used for fax transmission (ITU-T T.4/T.6) and is still used in bitmap image formats (PCX, BMP with RLE compression) and in some TIFF compression modes. RLE is most effective on data with long runs of repeated values, such as simple graphics, logos, and scanned documents. It is less effective on text with few repetitions, where it can actually increase file size (e.g., `ABC` becomes `1A1B1C`, which is twice as long). There are many variants of RLE: some use a threshold before encoding a run (e.g., only encode runs of 4+), some use escape characters to distinguish literal counts from data, and some use byte-pair encoding for binary data. This tool implements the simplest form: every run of one or more identical characters is encoded as count+character.
How to use this tool
- Select Encode or Decode mode using the toggle buttons.
- For encoding: enter text with repeated characters (e.g., `AAAAABBBCC`).
- For decoding: enter RLE-encoded text (e.g., `5A3B2C`).
- View the result in the output field and check the compression ratio.
- Use the Copy button to copy the result to your clipboard.
Real-world examples
Encoding repeated characters
Input: `AAAAABBBCCCAAA`. Output: `5A3B3C3A`. The original is 14 characters, the encoded is 8 characters, giving a compression ratio of 57%.
Decoding RLE data
Input: `3A2B1C`. Output: `AAABBC`. The decoder reads each count+character pair and expands it.
When RLE makes data larger
Input: `ABCDEF` (no repeated characters). Output: `1A1B1C1D1E1F`. The encoded version is 12 characters vs 6 original, a 200% expansion. RLE is only effective on data with long runs.
Comparison with similar methods
| Method | Complexity | Typical use |
|---|---|---|
| RLE (this tool) | O(n) single pass | Simple graphics, fax, data with long runs |
| Huffman coding | O(n log n) with frequency table | Text compression, general-purpose |
| LZ77/LZ78 | O(n) with sliding window | DEFLATE (gzip, PNG), general-purpose |
| LZ-String | O(n) with dictionary | JavaScript text compression for localStorage |
Limitations or considerations
RLE only compresses data with long runs of identical characters. For typical text, RLE will often expand the data rather than compress it. This implementation uses the simplest RLE variant where every run is encoded, including runs of length 1. More sophisticated RLE variants use thresholds or escape characters to avoid expanding non-repetitive data. The decoder expects well-formed RLE input (alternating digits and characters); malformed input may produce unexpected results.
Frequently asked questions
When is RLE useful?
RLE is most useful for data with long runs of repeated values: simple bitmap images, logos, scanned documents, and certain types of sensor data. For general text, RLE typically makes data larger.
What is the compression ratio?
The compression ratio is (encoded length / original length). A ratio below 100% means the data was compressed. A ratio above 100% means RLE made the data larger.
Can RLE handle binary data?
This tool works on text (UTF-8 strings). Binary RLE variants exist for image and audio data, but they use byte-level encoding rather than character-level encoding.
Is RLE used in any real formats?
Yes. RLE is used in PCX and BMP image formats, TIFF compression mode 2, PDF streams, and was the basis for fax compression (ITU-T T.4/T.6). Modern formats like PNG use DEFLATE which incorporates LZ77, a more sophisticated compression method.
Conclusion
The RLE encoder/decoder is a simple but educational tool that demonstrates one of the oldest compression algorithms. While RLE is rarely used alone in modern systems, it remains important for understanding compression fundamentals and is still found in legacy image formats. For general-purpose text compression, use the Gzip, LZ-String, or Brotli tools on this site.