How to encode the string in redable format


I have python code that decode and the decompress the string and when i encode and compress the string the ouput is different

import zlib, base64
a = zlib.decompress(base64.b64decode('eF7zSM3JyVcIzy/KSVFU8FTPVcjPUwguSUzO9i9LLUrLyS9XBADJNQvD'))

The output will be b"Hello World! I’m on StackOverflow!"

a = zlib.compress(base64.b64encode(b"Hello World! I'm on StackOverflow!"))

but when i try to encode the above string b"Hello World! I’m on StackOverflow!" the output i am getting is byte string like this b'xx9cx0bvx0f+Nrxb7Hx0f3xb2xacLrx0fxccxf0txcdxceKnv*KxcatnIqwxcbJ4xb04x8ax8axf0xcaMrxb74xf6x0cxb4xb5x05x00x85lx0ff'

but i want to get output like this eF7zSM3JyVcIzy/KSVFU8FTPVcjPUwguSUzO9i9LLUrLyS9XBADJNQvD

This was example from Trying to decode zlib compressed and base64 encoded data to a readable format in Python

what is wrong in my code

Asked By: Rahul



You should Base64 encode the result of compressing, not the other way around.

print(base64.b64encode(zlib.compress(b"Hello World! I'm on StackOverflow!")))
Answered By: Unmitigated

This is how your code should look like

import zlib, base64

compressed_data = zlib.compress(b"Hello World! I'm on StackOverflow!")
encoded_data = base64.b64encode(compressed_data)



Note: Output is not exactly the same because compression algorithms are not guaranteed to produce same output for the same input.

Answered By: Jamiu S.
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.