【问题标题】:Data type somehow gets converted when appended to a list附加到列表时,数据类型会以某种方式被转换
【发布时间】:2021-11-27 19:04:16
【问题描述】:

我正在尝试使用 ASCII 编码将文本转换为二进制,然后将二进制解码回文本作为更大项目的概念证明。以下代码运行良好,并完成了它应该做的事情:

bytes = []

input_data = "Hello"

for b in input_data:
    new_byte = bin(int.from_bytes(b.encode('ascii'), 'big'))
    bytes.append(new_byte)

for x in bytes:
    print(x)

for byte in bytes:
    byte_int = int(byte, 2)
    output_data = str(byte_int.to_bytes(byte_int, 'big').decode('ascii'))
    print(output_data)

但是,一旦我尝试将output_data 的给定值附加到 for 循环内的列表中,由于某些奇怪的原因,字符串会转换为 HEX。

bytes = []
output_list = []

input_data = "Hello"

for b in input_data:
    new_byte = bin(int.from_bytes(b.encode('ascii'), 'big'))
    bytes.append(new_byte)

for x in bytes:
    print(x)

for byte in bytes:
    byte_int = int(byte, 2)
    output_data = str(byte_int.to_bytes(byte_int, 'big').decode('ascii'))
    print(output_data)
    output_list.append(output_data)

print(output_list)

输出:

0b1001000
0b1100101
0b1101100
0b1101100
0b1101111
H
e
l
l
o
['\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00H', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00e', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00l', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00l', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00o']

我不知道为什么会这样,有什么解释吗?

【问题讨论】:

    标签: python binary ascii


    【解决方案1】:

    您误用了int.to_bytes 方法。正如文档所述,

    int.to_bytes(长度, 字节序, *, 有符号=False)

    返回一个数组 表示整数的字节。 [...]

    整数表示为 长度字节。

    您使用了byte_int.to_bytes(byte_int, 'big'),因此您将例如整数 72 转换为长度为 72 的字节字符串,这会在表示您的值的字节之前为您提供 71 个空字节。

    你想要一个字节,所以使用

    output_data = str(byte_int.to_bytes(1, 'big').decode('ascii'))
    

    【讨论】:

    • 啊,愚蠢的错误,谢谢指出!
    【解决方案2】:

    当您打印list 对象本身而不是其内容时,实际内容不会以表示方式打印。这就是您看到 HEX 值的原因。

    要实现你想要的,只需解压列表:

    print(*output_data)
    

    或者这样:

    for item in output_list:
        print(item, end=' ')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-18
      • 2016-02-01
      • 2013-11-18
      • 2018-08-27
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 2017-12-01
      相关资源
      最近更新 更多