简体   繁体   English

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

[英]Binary to String/Text in Python

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.但是,这种方法对我不起作用,因为据说“b”不存在,而且我不确定如何用变量替换“消息”。 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?那么如何将二进制字符串转换为文本字符串呢?

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

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() : 您可以获取每个8个字符的块(一个字节),将其转换为整数,然后将其转换为带有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. 假设字符串中编码的值是ASCII,这将为您提供字符。 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

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): 通常你会将传入的字符串转换为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)

Provide the optional base argument to int to convert: int提供可选的base参数以进行转换:

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

In Python 2, an ascii-encoded (byte) string is also a utf8-encoded (byte) string. 在Python 2中,ascii编码(字节)字符串也是utf8编码(字节)字符串。 In Python 3, a (unicode) string must be encoded to utf8-encoded bytes. 在Python 3中,必须将(unicode)字符串编码为utf8编码的字节。 The decoding example was going the wrong way. 解码的例子走错了路。

>>> 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”的字符串是特殊情况,并且适用相同的规则。

To convert bits given as a "01"-string (binary digits) into the corresponding text in Python 3: 要将作为“01”-string(二进制数字)给出的位转换为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,反之亦然

A working code for python 3 python 3 的工作代码

Binstr = '00011001 00001000'
Binstr.split(' ')
s = []
for i in Binstr:
    s.append(chr(i))
print(''.join(s))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM