Python 中的二进制到字符串/文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40557335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:39:51  来源:igfitidea点击:

Binary to String/Text in Python

pythonpython-2.7python-3.xtextbinary

提问by Dan

I have searched many times online and I have not been able to find a way to convert my binary string variable, X

我在网上搜索了很多次,但找不到一种方法来转换我的二进制字符串变量X

X = "1000100100010110001101000001101010110011001010100"

into a UTF-8 string value.

转换为 UTF-8 字符串值。

I have found that some people are using methods such as

我发现有些人正在使用诸如

b'message'.decode('utf-8')

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?

但是,这种方法对我不起作用,因为据说“b”不存在,而且我不确定如何用变量替换“消息”。不仅如此,我还无法理解这种方法是如何工作的。有更好的选择吗?

So how could I convert a binary string into a text string?

那么如何将二进制字符串转换为文本字符串呢?

EDIT: I also do not mind ASCII decoding

编辑:我也不介意 ASCII 解码

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

采纳答案by mhawke

It looks like you are trying to decode ASCII characters from a binary string representation (bit string) of each character.

看起来您正在尝试从每个字符的二进制字符串表示(位串)中解码 ASCII 字符。

You can take each block of eight characters (a byte), convert that to an integer, and then convert that to a character with chr():

您可以将每个包含八个字符(一个字节)的块转换为整数,然后使用以下命令将其转换为字符chr()

>>> X = "0110100001101001"
>>> print(chr(int(X[:8], 2)))
h
>>> print(chr(int(X[8:], 2)))
i

Assuming that the values encoded in the string are ASCII this will give you the characters. You can generalise it like this:

假设字符串中编码的值是 ASCII,这将为您提供字符。你可以这样概括:

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

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 unicodestring and that can be done like this (Python 2):

如果您想将其保留在原始编码中,则无需进一步解码。通常,您会将传入的字符串转换为 Python unicode字符串,并且可以这样做(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)

回答by jfs

To convert bits given as a "01"-string (binary digits) into the corresponding text in Python 3:

要将作为“01”字符串(二进制数字)给出的位转换为 Python 3 中的相应文本:

>>> bits = "0110100001101001"
>>> n = int(bits, 2)
>>> n.to_bytes((n.bit_length() + 7) // 8, 'big').decode()
'hi'

For Python 2/3 solution, see Convert binary to ASCII and vice versa.

对于 Python 2/3 解决方案,请参阅将二进制转换为 ASCII,反之亦然

回答by souldeux

Provide the optional base argument to intto convert:

提供可选的基本参数以int进行转换:

>> x = "1000100100010110001101000001101010110011001010100"
>> int(x, 2)
301456912901716

回答by Terry Jan Reedy

In Python 2, an ascii-encoded (byte) string is also a utf8-encoded (byte) string. In Python 3, a (unicode) string must be encodedto utf8-encoded bytes. The decoding example was going the wrong way.

在 Python 2 中,ascii 编码(字节)字符串也是 utf8 编码(字节)字符串。在 Python 3 中,(unicode)字符串必须编码为 utf8 编码的字节。解码示例走错了路。

>>> X = "1000100100010110001101000001101010110011001010100"
>>> X.encode()
b'1000100100010110001101000001101010110011001010100'

Strings containing only the digits '0' and '1' are a special case and the same rules apply.

仅包含数字“0”和“1”的字符串是一种特殊情况,适用相同的规则。