什么是“&"在python bytearray中代表 [英] What does a '&' stand for in a python bytearray

查看:159
本文介绍了什么是“&"在python bytearray中代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python bytearray末尾的&符号&是什么意思?

What does an ampersand & mean at the end of a python bytearray?

例如:

x_w = bytearray(b'\x00\x00\x04\x12\xaa\x12\x12&')

通过

int.from_bytes(x_w, 'little')

Out[1]: 2743275644678045696

与没有'&'的相同字节数组给出的结果不同:

it gives a different result from the same bytearray without the '&':

x_wo = bytearray(b'\x00\x00\x04\x12\xaa\x12\x12')
int.from_bytes(x_wo, 'little')

Out[2]: 5087071236784128

我检查了文档,但没有找到一个答案.谢谢!

I checked the documentation but haven't found an answer to this. Thanks!

推荐答案

它只是值26(十进制38)的字节的表示形式,它是ASCII中的'&'字符.

It's simply the representation of the byte with value 26 (decimal 38), which is the '&' character in ASCII.

如果您打印所使用的字节文字的实际字节值,则可以清楚地看到这一点:

If you print the actual byte values of the bytes literal you used, you can see this clearly:

>>> print(' '.join('%02x' % b for b in b'\x00\x00\x04\x12\xaa\x12\x12&'))
00 00 04 12 aa 12 12 26

bytearray对象的repr倾向于使用ASCII字符而不是十六进制转义符来表示字节.因此,即使它们在技术上是等效的,它也会更喜欢'&'而不是'\x26'表示形式:

And the repr of the bytearray object prefers to represent bytes using ASCII characters rather than hex escapes whenever possible. So it will prefer the representation '&' rather than '\x26', even though they are technically equivalent:

>>> bytearray([0x00, 0x00, 0x04, 0x12, 0xAA, 0x12, 0x12, 0x26])
bytearray(b'\x00\x00\x04\x12\xaa\x12\x12&')

>>> b'\x26' == b'&'
True

这篇关于什么是“&"在python bytearray中代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆