Tool for encoding / decoding with Run-Length Encoding (RLE), a very basic data compression algorithm that consists in describing a string according to its repetitions.
RLE (Run-Length Encoding) - dCode
Tag(s) : Compression
dCode is free and its tools are a valuable help in games, maths, geocaching, puzzles and problems to solve every day!
A suggestion ? a feedback ? a bug ? an idea ? Write to dCode!
Run Length Encoding (or RLE, or range encoding) is a data compression (lossless) technique based on successive repetitions of elements.
Run Length means run length , and indeed, with RLE, what matters is the length, the size of the repetitions (in a text, a message, etc.)
RLE is a generic name for this compression technique, there are several ways to use and implement it but to simplify, RLE encodes a sequence of identical values by a single value followed by the number of repetitions.
The text to be encoded is scanned to find sequences of identical characters, then note the character and the number of repetitions in the sequence.
Example: DDDDDCCCCOOODDE can be described as 5 times the D character followed by 4 times the C character, etc. The message can therefore be compressed D5C4C3D2E1 (10 characters instead of 15, compression rate: 33%).
This procedure only produces compression if the message consists of many repetitions.
Example: DCODE would be compressed D1C1O1D1E1 (10 characters instead of 5, compression rate: -100%). In order to avoid this kind of case, it is possible to omit the 1 in the case of non-repetitions.
Writing variant
It is possible to encode by reversing the characters and the counts.
Example: 5D4C3C2D1E is then equivalent to D5C4C3D2E1
In case of Binary data
If the message is composed of binary data (0 and 1), then it is possible to use RLE without indicating the character, the numbers are sufficient. The first number indicating the 0 (or the 1), then alternatively.
Example: 000111100000 is coded 3,4,5
Case of Numerical data
If the message is made up of numbers, then use a separator otherwise it will no longer be possible to distinguish the characters from their number of repetitions.
Example: 11111111111122 is coded as '12-1,2-2 'and not' 12122 'which could be translated as' 1' repeated 2122 times, or 1 times 2 followed by 12 times the number 2, etc.
The RLE decompression consists in browsing the message formed of pairs (character, number of repetition) and writing the equivalent text by writing the character the corresponding number of times.
Example: D5C4C3D2E1 decomposes into D5, C4, O3, D2, C1 and repeats the characters the correct number of times: D5 => DDDDD > CCCC, etc. To get DDDDDCCCCOOODDE
Binary data case
Example: 3,4,5 is decoded 00011110000 (or 11100001111 depending on the convention used)
A message compressed with RLE is composed of pairs (Character-Number) or triples (Character-Separator-Number).
Bitmap BMP and PCX image formats use RLE to reduce file size.
RLE is particularly effective for data with long, repeating sequences (or identical pattern). Binary images, images with solid color areas (such as simple logos), or text files with repeating characters often benefit from effective compression with RLE.
Conversely, RLE is not effective for data with no repetitions or few repetitions. In these cases, the size of the compressed data may even increase compared to the original size.
RLE can be combined with other compression algorithms to improve performance (more compact files).
The source code of the RLE function for encoding:
function RLE_Encode(input_string) {
encoded_string = ""
count = 1
for (i = 1; i < length(input_string); i++) {
if (input_string[i] == input_string[i - 1]) {
count = count + 1
}
else {
encoded_string += input_string[i - 1] + count
count = 1
}
}
encoded_string += input_string[i - 1] + count
return encoded_string
}
The decoding function is programmed as follows:
function RLE_Decode(encoded_string) {
decoded_string = ""
for (i = 0; i < length(encoded_string); i+=2) {
char = encoded_string[i]
count = encoded_string[i+1]
decoded_string += string_repeat(char, count)
}
return decoded_string
}
dCode retains ownership of the "RLE (Run-Length Encoding)" source code. Except explicit open source licence (indicated Creative Commons / free), the "RLE (Run-Length Encoding)" algorithm, the applet or snippet (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, breaker, translator), or the "RLE (Run-Length Encoding)" functions (calculate, convert, solve, decrypt / encrypt, decipher / cipher, decode / encode, translate) written in any informatic language (Python, Java, PHP, C#, Javascript, Matlab, etc.) and all data download, script, or API access for "RLE (Run-Length Encoding)" are not public, same for offline use on PC, mobile, tablet, iPhone or Android app!
Reminder : dCode is free to use.
The copy-paste of the page "RLE (Run-Length Encoding)" or any of its results, is allowed (even for commercial purposes) as long as you credit dCode!
Exporting results as a .csv or .txt file is free by clicking on the export icon
Cite as source (bibliography):
RLE (Run-Length Encoding) on dCode.fr [online website], retrieved on 2024-12-21,