如何将字节列表从十进制更改为十六进制?

发布于 2025-01-18 19:55:01 字数 1266 浏览 3 评论 0原文

基本上,我有一个字节列表,我想将其转换为 ASCII 文本,

因此我将该列表转换为单个字节数组(如果这是错误的术语,我很抱歉),然后能够将其转换为 ASCII 文本,

但从那以后列表的值被视为十进制,我在将其转换为文本时遇到问题,因为我需要它们为十六进制。 (obs:这些值是十六进制的,它们只是没有表示为它)

好的,所以这是初始列表:

text_bytes_list = [b'74', b'68', b'69', b'73', b'20', b'69', b'73', b'20', b'61', b'20', b'74', b'65', b'73', b'74']

我这样做是为了将所有内容都放在一个字节中:

text_byte = b''.join(text_byte_list)
print(text_byte)

输出是:b'7468697320697320612074657374'

所以我想要的是一种将这个:

text_bytes_list = [b'74', b'68', b'69', b'73', b'20', b'69', b'73', b'20', b'61', b'20', b'74', b'65', b'73', b'74']

变成这个:

text_bytes_list = [b'0x74', b'0x68', b'0x69', b'0x73', b'0x20', b'0x69', b'0x73', b'0x20', b'0x61', b'0x20', b'0x74', b'0x65', b'0x73', b'0x74']

所以当我加入列表时,使用相同的过程我会得到:

text_byte = b''.join(text_byte_list)
print(text_byte)

输出: b'\x74\x68\x69\x73\x20\x69\x73\x20\x61\x20\x74\x65\x73\x74'

使用该值我可以将其转换为文本(我的目标在这里),这样做:

text_byte = b'\x74\x68\x69\x73\x20\x69\x73\x20\x61\x20\x74\x65\x73\x74'
print(text_byte.decode("ascii"))

输出应该是:这是一个测试

抱歉,帖子有点大,但我很感谢我能得到的任何帮助,请帮忙:)

basically, I have a list of bytes, and I want to convert that to ASCII text

so I converted that list into a single bytearray (I apologize if that's the wrong term), to then be able to convert it into ASCII text

but since that list has the values seen as decimal, I have issues to convert it to text, since i would need them to be in hex. (obs: the values are in hex, they are just not represented as it)

ok, so this is the initial list:

text_bytes_list = [b'74', b'68', b'69', b'73', b'20', b'69', b'73', b'20', b'61', b'20', b'74', b'65', b'73', b'74']

and I did this to get it all in a single byte:

text_byte = b''.join(text_byte_list)
print(text_byte)

the output is: b'7468697320697320612074657374'

so what I wanted was a way to turn this:

text_bytes_list = [b'74', b'68', b'69', b'73', b'20', b'69', b'73', b'20', b'61', b'20', b'74', b'65', b'73', b'74']

into this:

text_bytes_list = [b'0x74', b'0x68', b'0x69', b'0x73', b'0x20', b'0x69', b'0x73', b'0x20', b'0x61', b'0x20', b'0x74', b'0x65', b'0x73', b'0x74']

so when I joined the list, using the same process I would get:

text_byte = b''.join(text_byte_list)
print(text_byte)

output: b'\x74\x68\x69\x73\x20\x69\x73\x20\x61\x20\x74\x65\x73\x74'

with that value I can convert it into text (my objective here), doing this:

text_byte = b'\x74\x68\x69\x73\x20\x69\x73\x20\x61\x20\x74\x65\x73\x74'
print(text_byte.decode("ascii"))

the output should be: this is a test

sorry, the post got a little big, but I appreciate any help I can get, pls help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

別甾虛僞 2025-01-25 19:55:01

只需将它们转换为整数,然后转换为“字节”字符串即可。

>>> text_bytes_list = [b'74', b'68', b'69', b'73', b'20', b'69', b'73', b'20', b'61', b'20', b'74', b'65', b'73', b'74']
>>> tbl = [int(t,16) for t in text_bytes_list]
>>> tbl
[116, 104, 105, 115, 32, 105, 115, 32, 97, 32, 116, 101, 115, 116]
>>> bytes(tbl)
b'this is a test'
>>>

Just convert them to integers, and then into a "bytes" string.

>>> text_bytes_list = [b'74', b'68', b'69', b'73', b'20', b'69', b'73', b'20', b'61', b'20', b'74', b'65', b'73', b'74']
>>> tbl = [int(t,16) for t in text_bytes_list]
>>> tbl
[116, 104, 105, 115, 32, 105, 115, 32, 97, 32, 116, 101, 115, 116]
>>> bytes(tbl)
b'this is a test'
>>>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文