How to encode the string in redable format
Question:
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'))
print(a)
The output will be b"Hello World! I’m on StackOverflow!"
a = zlib.compress(base64.b64encode(b"Hello World! I'm on StackOverflow!"))
print(a)
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
Answers:
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!")))
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)
print(encoded_data)
print(encoded_data.decode())
b'eJzzSM3JyVcIzy/KSVFU8FTPVcjPUwguSUzO9i9LLUrLyS9XBADJNQvD'
eJzzSM3JyVcIzy/KSVFU8FTPVcjPUwguSUzO9i9LLUrLyS9XBADJNQvD
Note: Output is not exactly the same because compression algorithms are not guaranteed to produce same output for the same input.
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'))
print(a)
The output will be b"Hello World! I’m on StackOverflow!"
a = zlib.compress(base64.b64encode(b"Hello World! I'm on StackOverflow!"))
print(a)
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
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!")))
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)
print(encoded_data)
print(encoded_data.decode())
b'eJzzSM3JyVcIzy/KSVFU8FTPVcjPUwguSUzO9i9LLUrLyS9XBADJNQvD'
eJzzSM3JyVcIzy/KSVFU8FTPVcjPUwguSUzO9i9LLUrLyS9XBADJNQvD
Note: Output is not exactly the same because compression algorithms are not guaranteed to produce same output for the same input.