Run Length Encoding Calculator
Compress a string by replacing repeated characters with counts.
What it does
Run length encoding is one of the simplest compression methods. Instead of storing every repeated character, it stores the character once together with a count. A run of four As becomes A4 rather than AAAA.
Kansas State University's introductory computer science textbook explains the idea with a string of repeated characters and shows how each run is replaced by a shorter representation. Carnegie Mellon University algorithm notes give the same example and note that RLE is used as a step inside larger compression systems.
This tool scans the input from left to right, groups adjacent identical characters and outputs each group as a character followed by its count, separated by spaces. It reports the number of runs and compares the original and encoded string lengths.
The method is lossless: you can rebuild the original exactly from the pairs. It is most effective on data with long runs, such as simple graphics or repeated sensor values.
For this teaching version, spaces separate the pairs and counts are written in ordinary decimal form. Production formats use binary codes and special handling for ambiguous cases.
How to use
- Type or paste a string of characters into the input box.
- Read the encoded string, which lists each run as character followed by count.
- Check the number of runs, which equals the number of output pairs.
- Compare the original and encoded lengths.
- Try a string with long runs to see a shorter encoded result.
- Try alternating characters to see the encoded version grow.
Formula
- run
- A maximal sequence of identical adjacent characters.
- c
- The character that repeats.
- k
- The number of times the character repeats in the run.
- pair
- The compact representation of one run.
The engine scans once through the input and counts each run before emitting it, so the work grows linearly with the input length. It accepts any characters up to the 1,024 character limit.
Worked example
Example: a simple string
Input string AAAABBBCCDAABBB.
- The first run is four As, giving the pair A4.
- The next run is three Bs, giving B3.
- Then two Cs give C2 and one D gives D1.
- Two As give A2 and three Bs give B3.
- The encoded string is A4 B3 C2 D1 A2 B3.
- There are 6 runs, and the tool reports original length 15 and encoded length 17.
- In this small example the extra spaces mean the encoded string is longer. Longer runs would help.
Reference table
| Run | Character | Count |
|---|---|---|
| 1 | A | 4 |
| 2 | B | 3 |
| 3 | C | 2 |
| 4 | D | 1 |
| 5 | A | 2 |
| 6 | B | 3 |
Limits
- The input is limited to 1,024 characters.
- This version uses plain text counts and spaces rather than a binary format.
- Single character runs make the output longer than the input.
- It does not handle escape rules for characters that look like digits.
Common mistakes
- Expecting compression on data with no repeated adjacent characters.
- Using a string with digits and then assuming the pairs have a fixed width.
- Forgetting that runs must be adjacent rather than merely present somewhere in the string.
- Comparing encoded length in characters with a binary file size in bytes.
- Assuming the method is secure. It is compression, not encryption.
Try it yourself
Quick checks. Answers sit tucked under each question.
Encode AAAABBBCCDAABBB.
A4 B3 C2 D1 A2 B3, with 6 runs.
What input gives the best compression?
Long runs of the same character, because one pair replaces many characters.
Is the encoding lossless?
Yes. The character and count pairs rebuild the original string exactly.
Questions
What is run length encoding?
A compression method that stores each run of identical characters as a character and a count.
When is it useful?
On data with long runs, such as simple images or repeated values.
Does it always shrink data?
No. Alternating characters make the encoded form larger.
Is it lossless?
Yes, the original data can be reconstructed exactly.
Is this the format used in image files?
The idea is used inside many formats, though production versions use binary codes.
Can it encrypt data?
No. It provides no secrecy at all.
Sources
- Kansas State University, Intro CS Textbook: Run Length Encoding
- Carnegie Mellon University, 15-750 Algorithms: Compression lecture