Python中的二进制到字符串/文本 [英] Binary to String/Text in Python
问题描述
我已经在线搜索了很多次,但找不到一种方法来转换我的二进制字符串变量 X
I have searched many times online and I have not been able to find a way to convert my binary string variable, X
X = "1000100100010110001101000001101010110011001010100"
转换为UTF-8字符串值.
into a UTF-8 string value.
我发现有些人正在使用诸如
I have found that some people are using methods such as
b'message'.decode('utf-8')
但是,这种方法对我不起作用,因为据说不存在"b",而且我不确定如何用变量替换消息".不仅如此,我还无法理解这种方法的工作原理.有更好的选择吗?
however, this method has not worked for me, as 'b' is said to be nonexistent, and I am not sure how to replace the 'message' with a variable. Not only, but I have not been able to comprehend how this method works. Is there a better alternative?
那么如何将二进制字符串转换为文本字符串?
So how could I convert a binary string into a text string?
我也不介意ASCII解码
I also do not mind ASCII decoding
澄清:这就是我想发生的事情.
CLARIFICATION: Here is specifically what I would like to happen.
def binaryToText(z):
# Some code to convert binary to text
return (something here);
X="0110100001101001"
print binaryToText(X)
然后将产生字符串...
This would then yield the string...
hi
推荐答案
您似乎正在尝试从每个字符的二进制字符串表示形式(位字符串)中解码ASCII字符.
It looks like you are trying to decode ASCII characters from a binary string representation (bit string) of each character.
您可以将每个块包含八个字符(一个字节),将其转换为整数,然后使用chr()
将其转换为字符:
You can take each block of eight characters (a byte), convert that to an integer, and then convert that to a character with chr()
:
>>> X = "0110100001101001"
>>> print(chr(int(X[:8], 2)))
h
>>> print(chr(int(X[8:], 2)))
i
假设字符串中编码的值是ASCII,这将为您提供字符.您可以将其概括如下:
Assuming that the values encoded in the string are ASCII this will give you the characters. You can generalise it like this:
def decode_binary_string(s):
return ''.join(chr(int(s[i*8:i*8+8],2)) for i in range(len(s)//8))
>>> decode_binary_string(X)
hi
如果您想将其保留为原始编码,则无需进一步解码.通常,您会将输入的字符串转换成Python的 unicode 字符串,可以这样做(Python 2):
If you want to keep it in the original encoding you don't need to decode any further. Usually you would convert the incoming string into a Python unicode string and that can be done like this (Python 2):
def decode_binary_string(s, encoding='UTF-8'):
byte_string = ''.join(chr(int(s[i*8:i*8+8],2)) for i in range(len(s)//8))
return byte_string.decode(encoding)
这篇关于Python中的二进制到字符串/文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!