Python 3.x 将二进制文件转换为字符串
我需要将:1010100 1101000 1101001 1110011 100000 1101001 1110011 100000 1100001 100000 1110011 1110100 1110010 1101001 1101110 1100111转换为字符串。 我已将字符串转换为二进制Python 3.x 将二进制文件转换为字符串,python-3.x,binary,Python 3.x,Binary,我需要将:1010100 1101000 1101001 1110011 100000 1101001 1110011 100000 1100001 100000 1110011 1110100 1110010 1101001 1101110 1100111转换为字符串。 我已将字符串转换为二进制 text2 = 'This is a string' res = ' '.join(format(ord(i), 'b') for i in text2) print(res) #output:
text2 = 'This is a string'
res = ' '.join(format(ord(i), 'b') for i in text2)
print(res)
#output:
1010100 1101000 1101001 1110011 100000 1101001 1110011 100000 1100001 100000 1110011 1110100 1110010 1101001 1101110 1100111
现在我无法将其转换回字符串,chr给了我一些亚洲字符:
c = ' '.join(chr(int(val)) for val in res.split(' '))
print(c)
#output:
You need to set the base
for int
:
''.join(chr(int(val, 2)) for val in res.split(' '))
c='''.join(chr(int(val))代表res.split('')中的val)
印刷品(c)
#输出:
您需要为int
设置base
:
'This is a string'
输出:
“也不工作”是什么意思?@ScottHunter你可以看到输出,它仍然不是“这是一个字符串”。这个基数“2”是什么?我试图找到更多的信息,但无法找到。