I need to do some work with bytes in Python and I've come across a byte string I don't really understand:

b"H\x00\x84\xffQ\x00\xa6\xff+\x00\x96\xff\xc2\xffI\xff\xa5\xff'\xff\x8a\xff\x19\xff\x19\xff\xf6\xfe\xb0\xfe\xc7\xfeJ\xfel\xfe\xf8\xfd+\xfe\xef\xfd:\xfe\xc3\xfd*\xfe_\xfd\xdf\xfd\n\xfd\xa3\xfd\xc6\xfcq\xfd\xbd\xfc?\xfd"

So according to what I know, bytes should be represented as \xhh, where hh are hexadecimal values (from 0 to f). However, in the third segment, there is \xffQ, and farther on there are other characters which shouldn't appear: I, ', *, :, ? etc. I've used hex() method to see what would be the outcome, and I got this:

480084ff5100a6ff2b0096ffc2ff49ffa5ff27ff8aff19ff19fff6feb0fec7fe4afe6cfef8fd2bfeeffd3afec3fd2afe5ffddffd0afda3fdc6fc71fdbdfc3ffd

As you can see, some parts of the hex are the same, but e.g. \xffQ was changed into ff51. I need to append some data to this byte string, so I'd like to know what's going on there (or how to get the same result).

推荐答案

Both repr and str when processing a bytes object will print ASCII characters where possible. Otherwise their hexadecimal values will be shown in the form \xNN.

It might help you to visualise the content if you print it as all hexadecimal as follows:

b = b"H\x00\x84\xffQ\x00\xa6\xff+\x00\x96\xff\xc2\xffI\xff\xa5\xff'\xff\x8a\xff\x19\xff\x19\xff\xf6\xfe\xb0\xfe\xc7\xfeJ\xfel\xfe\xf8\xfd+\xfe\xef\xfd:\xfe\xc3\xfd*\xfe_\xfd\xdf\xfd\n\xfd\xa3\xfd\xc6\xfcq\xfd\xbd\xfc?\xfd"
print(''.join(hex(b_) for b_ in b))

Output:

0x480x00x840xff0x510x00xa60xff0x2b0x00x960xff0xc20xff0x490xff0xa50xff0x270xff0x8a0xff0x190xff0x190xff0xf60xfe0xb00xfe0xc70xfe0x4a0xfe0x6c0xfe0xf80xfd0x2b0xfe0xef0xfd0x3a0xfe0xc30xfd0x2a0xfe0x5f0xfd0xdf0xfd0xa0xfd0xa30xfd0xc60xfc0x710xfd0xbd0xfc0x3f0xfd

Python相关问答推荐

如何在 python 中将多个数据集添加到单个图中?

以一种奇怪的方式对Pandas 数据框进行排序和分组

如何从日期时间对象中删除日期

从对象中的 JMESPath 表达式中提取键

如何将列表中的每个字典转换为python中的嵌套字典?

Pandas 数据框替换列的多个子字符串

使用 excel 值在 python 中执行计算

struct.pack(f'{len(x)}s', x) 有什么作用吗?

Pandas:两个不同数据帧中的日期时间比较以找到最接近的匹配项

查找掩码的周长坐标

只获取包含某个列表列的用户

获取行值作为列值

合并多个 BatchEncoding 或从 BatchEncoding 对象列表创建 tensorflow 数据集

为什么我收到 TypeError:dtype datetime64[ns] 无法转换为 timedelta64[ns]?

PySpark - 有限制的累积和

Python pandas合并/加入复制数据以进行新/旧数据输入

如何用 2 * 3 块占用 10 x 10 块,不让其他人推另一个 2 * 3

如何从两个不同文件中的两个不同类中获取函数以相互通信?

Raspbian-Python3.9:ImportError:未安装 _imagingft C 模块

如果以某个字符开头,则从列中的列表中删除元素