如何在不导入任何库的情况下将二进制文件转换为python 3中的text或ascii? 例如,我将其存储在变量
中0000000000000000000000000000000000000000010101000100100001000101001000000101001101000011010000010101001001001100010001010101010000100000010100000100100101001101010100000100010101010010010011100100010101001100000011010000101000001101000010100100001101001000010000010101000001010100010001010101001000100000010010010010000001010000010000010101001001001001010100110011101000100000010100110100010101010000010101000100010101001101010000100100010101010010001011000010000000110001001101110011100100110010
答案 0 :(得分:1)
将输入值切换为8个比特的块并使用int()
将其转换为整数,然后chr()
将其转换为字符:
for i in range(0, len(inputstring), 8):
print(chr(int(inputstring[i:i + 8], 2)), end='')
会这样做。
演示:
>>> inputstring = b'0000000000000000000000000000000000000000010101000100100001000101001000000101001101000011010000010101001001001100010001010101010000100000010100000100100101001101010100000100010101010010010011100100010101001100000011010000101000001101000010100100001101001000010000010101000001010100010001010101001000100000010010010010000001010000010000010101001001001001010100110011101000100000010100110100010101010000010101000100010101001101010000100100010101010010001011000010000000110001001101110011100100110010'
>>> for i in range(0, len(inputstring), 8):
... print(chr(int(inputstring[i:i + 8], 2)), end='')
...
THE SCARLET PIMPERNEL
CHAPTER I PARIS: SEPTEMBER, 1792>>>