如何将字节列表从十进制更改为十六进制?
基本上,我有一个字节列表,我想将其转换为 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将它们转换为整数,然后转换为“字节”字符串即可。
Just convert them to integers, and then into a "bytes" string.