"When converting bytes, there is a possibility of losing binary data. Here is a very reproducible example: >>print(bytes(b’\x08\x01\x10\xb6\x93\x9d\xf3\x84\x31\x1a\x10\x64\x61\x39\x38\x36\x33\x38\x61\x30\x38\x31\x64\x31\x36\x63\x66’)).
The printed result is b’\x08\x01\x10\xb6\x93\x9d\xf3\x841\x1a\x10da98638a081d16cf’, but the actual >>result should be b’\x08\x01\x10\xb6\x93\x9d\xf3\x84\x31\x1a\x10da98638a081d16cf’.
The bytes ‘\x84’ and ‘\x31’ have merged together. How can I solve this problem? I can’t avoid this issue whenever I use bytes."
I found out when using. read() with open to read files, it will automatically become like this
There’s no loss, it’s just the way they’re printed.
When printing bytestrings, printable characters within the ASCII range 0x20-0x7E are shown as the character (except for b'\x5C', which is shown as b'\\'), e.g. b'\x20' is shown as b' ', b'\x66' is shown as b'f' and b'\x31' is shown as b'1'.
No, they haven’t. The \x84 in the input literal becomes \x84 in the output representation, and the \x31 in the input literal becomes 1 in the output representation. It means the same actual underlying data either way.
Here is a simpler reproduction:
>>> b'\x31'
b'1'
>>> b'\x31' == b'1'
True
Nothing has been lost. It is two different ways to write the same thing - a bytes object that represents a single byte with the numeric value of forty-nine: