The binascii
is a widely used Python library for ASCII-encoded binary representations. It contains several methods for converting to binary from ASCII or hex, and vice versa.
binascii.rledecode_hqx is one of the methods of binascii
library in Python. It is the opposite of rlecode_hqx
method.
We can use binascii.rledecode_hqx
to perform RLE-decompression on data. We can use this method to decompress the data – previously compressed by using Run length encoding format – to original data.
The procedure is simple. While encoding the data, we use the method of rlecode_hqx
rlecode_hqx
iterates through the string to check for consecutive characters. In this case, there are 8 consecutive “o’s”. This will be compressed and output will be shown with a single “o” followed by \x90\x
and then the number of times it occurred.rledecode_hqx
identifies \x90\x
in the compressed input to find the number of times a single character is present in the uncompressed string and the decompresses the compressed string by replacing the number of characters with the actual characters.
binascii.rledecode_hqx(data)
Below are a few examples to illustrate the use of binascii.rledecode_hqx
.
import binascii# data in compressed format# there is '\x90\x' after o.# this is followed by a 07.# This means there are a total of 7 o'sdata = b'Hello\x90\x07W\x90\x06orld\x90\x04'# perform the decoding of datadecompressed = binascii.rledecode_hqx(data)print(decompressed)# another exampledata = b'Hi\x90\x04 Hello\x90\x04'decompressed = binascii.rledecode_hqx(data)print(decompressed)
Free Resources