Python Bytearray 打印 [英] Python Bytearray Printing

查看:44
本文介绍了Python Bytearray 打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 中有一个整数列表,它应该对应于以下 int 值(可以更改为十六进制字节值):

I have an integer list in Python that should correspond to the following int values (which can be changed to hex byte values):

[10, 145, 140, 188, 212, 198, 210, 25, 152, 20, 120, 15, 49, 113, 33, 220, 124, 67, 174, 224, 220, 241, 241]

但是,当我将该列表转换为字节数组(使用 bytearray(nameOfList))时,我得到以下打印输出.

However, when I convert that list to a bytearray (using bytearray(nameOfList)), I get the following printout.

bytearray(b'\n\x91\x8c\xbc\xd4\xc6\xd2\x19\x98\x14x\x0f1q!\xdc|C\xae\xe0\xdc\xf1\xf1')

我可以从这个字节数组中提取正确的值,不管它是如何打印的,但是字节数组的打印输出不应该对应于字节数组的十六进制值吗?(我的意思是,它似乎主要遵循十六进制值,直到 \x0f 之后,它开始发出胡言乱语......)

I can pull the correct values from this byte array, regardless of how it prints, but shouldn't the bytearray printout correspond to the hex values of the byte array? (I mean, it seems to mostly follow the hex values up until after \x0f, where it starts putting out gibberish...)

推荐答案

>>> x = bytearray(b'\n\x91\x8c\xbc\xd4\xc6\xd2\x19\x98\x14x\x0f1q!\xdc|C\xae\xe0
\xdc\xf1\xf1')
>>> import binascii
>>> print binascii.hexlify(x)
0a918cbcd4c6d2199814780f317121dc7c43aee0dcf1f1

如果您希望将所有内容都打印为十六进制字符串,请使用 binascii

Use binascii if you want all of it to be printed as a hex string

这篇关于Python Bytearray 打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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