如何在python中将二进制字符串转换为ascii字符串? [重复]

时间:2021-02-03 18:23:18

This question already has an answer here:

这个问题在这里已有答案:

I've made a little python program that reads binary from a file and stores it to a text file, read the text file and store the binary. But, I can't get the binary to work... it reads the files like this:

我做了一个小python程序,从文件读取二进制文件并将其存储到文本文件,读取文本文件并存储二进制文件。但是,我无法使二进制文件工作......它读取这样的文件:

f_bin = open(bin_file,"rb")
to_bin_data = f_bin.read()
bin_data = bin(reduce(lambda x, y: 256*x+y, (ord(c) for c in to_bin_data), 0))
f_bin.close()

this one doesen't work for me... Convert binary to ASCII and vice versa

这个对我不起作用...将二进制转换为ASCII,反之亦然

Something like this webpage: http://www.roubaixinteractive.com/PlayGround/Binary_Conversion/Binary_To_Text.asp

像这样的网页:http://www.roubaixinteractive.com/PlayGround/Binary_Conversion/Binary_To_Text.asp

Edit: I've now made a long if else script for it, but thanks for the answers

编辑:我现在为它做了一个很长的if if脚本,但感谢你的答案

3 个解决方案

#1


11  

Let's take the word 'hello' which is 0110100001100101011011000110110001101111

我们来一个'你好'这个词是0110100001100101011011000110110001101111

To translate that back to characters we can use chr and int (with a base of 2) and some list slicing...

要将其转换回字符,我们可以使用chr和int(基数为2)和一些列表切片......

''.join(chr(int(bin_text[i:i+8], 2)) for i in xrange(0, len(bin_text), 8))

If we wanted to take 'hello' and convert it to binary we can use ord and string formatting...

如果我们想要'hello'并将其转换为二进制,我们可以使用ord和字符串格式化...

''.join('{:08b}'.format(ord(c)) for c in 'hello')

#2


2  

Maybe you can use built-in functions:

也许你可以使用内置函数:

>>> myString = "hello"
>>> ba = bytearray(myString)
>>> ba[0]
104
>>> bin(ba[0])
'0b1101000'

Split the 0b:

拆分0b:

>>> bin(ba[0]).split('b')[1]
'1101000'

or

>>> bin(ba[0])[2:]
'1101000'

I'll hope you can solve your problem with the snippets! :)

我希望你能用这些片段解决你的问题! :)

#3


2  

I use the struct module:

我使用struct模块:

import struct
buf=struct.unpack('c',to_bin_data) # for one character
buf=struct.unpack('s',to_bin_data) # for a string 

edit: sorry, misunderstood the question... This works for binary data, not for strings of binary representaion of characters.

编辑:对不起,误解了这个问题......这适用于二进制数据,而不适用于二进制代表字符串的字符串。

#1


11  

Let's take the word 'hello' which is 0110100001100101011011000110110001101111

我们来一个'你好'这个词是0110100001100101011011000110110001101111

To translate that back to characters we can use chr and int (with a base of 2) and some list slicing...

要将其转换回字符,我们可以使用chr和int(基数为2)和一些列表切片......

''.join(chr(int(bin_text[i:i+8], 2)) for i in xrange(0, len(bin_text), 8))

If we wanted to take 'hello' and convert it to binary we can use ord and string formatting...

如果我们想要'hello'并将其转换为二进制,我们可以使用ord和字符串格式化...

''.join('{:08b}'.format(ord(c)) for c in 'hello')

#2


2  

Maybe you can use built-in functions:

也许你可以使用内置函数:

>>> myString = "hello"
>>> ba = bytearray(myString)
>>> ba[0]
104
>>> bin(ba[0])
'0b1101000'

Split the 0b:

拆分0b:

>>> bin(ba[0]).split('b')[1]
'1101000'

or

>>> bin(ba[0])[2:]
'1101000'

I'll hope you can solve your problem with the snippets! :)

我希望你能用这些片段解决你的问题! :)

#3


2  

I use the struct module:

我使用struct模块:

import struct
buf=struct.unpack('c',to_bin_data) # for one character
buf=struct.unpack('s',to_bin_data) # for a string 

edit: sorry, misunderstood the question... This works for binary data, not for strings of binary representaion of characters.

编辑:对不起,误解了这个问题......这适用于二进制数据,而不适用于二进制代表字符串的字符串。