What does the zlib.compress () method return in Python 3.x?

want to zlib compress a string of hexadecimal strings, such as "00FF00", and return the same hexadecimal string:

test = b"00FF00"
print(zlib.compress(test))

the result returns breadx\ x9c30ps30\ x00\ x00\ x04\ x90\ x01M" , which contains characters such as ps and M , which cannot be decoded decode ("UTF-8") , but will only report an error:

.
UnicodeDecodeError: "utf-8" codec can"t decode byte 0x9c in position 1: invalid start byte

query python.org/3/library/zlib.html-sharpzlib.compress" rel=" nofollow noreferrer "> Python Zlib official document can not find the reason, only know that the zlib.compress () method requires that data of type Bytes must be entered, and the result returns an object of type Bytes.
how do I convert the value returned by the method zlib.compress () into a hexadecimal string? Ask the great god for an answer.

Feb.28,2021

the result is still bytes. If you want to view the compressed result as a hexadecimal string

res = zlib.compress(test)

string = ['%02x' % b for b in res]
print(''.join(string))

use zlib.decompress to decompress


before decoding.

because zlib.compress () returns compressed bytes

you cannot decode compressed bytes

to get the 00FF00 string, you need to first call zlib.decompress () to decompress the compressed byte back to the original byte, and then decode the original byte

import zlib
origin_data = b'00FF00'
compress_data = zlib.compress(origin_data)
decompress_data = zlib.decompress(compress_data)
assert origin_data == decompress_data
decode_str = decompress_data.decode('utf-8')
print(decode_str)
Menu