Python 中的二进制到字符串/文本 [英] Binary to String/Text in Python
问题描述
我在网上搜索了很多次,都没有找到转换二进制字符串变量的方法,X
X = "1000100100010110001101000001101010110011001010100"
转换为 UTF-8 字符串值.
我发现有些人正在使用诸如
之类的方法b'message'.decode('utf-8')
但是,这种方法对我不起作用,因为据说b"不存在,而且我不确定如何用变量替换消息".不仅如此,我还无法理解这种方法是如何工作的.有没有更好的选择?
那么如何将二进制字符串转换为文本字符串?
我也不介意 ASCII 解码
澄清:这是我特别希望发生的事情.
def binaryToText(z):# 一些将二进制转换为文本的代码返回(这里的东西);X="0110100001101001"打印 binaryToText(X)
这将产生字符串...
嗨
您似乎正在尝试从每个字符的二进制字符串表示(位串)中解码 ASCII 字符.
您可以将每块 8 个字符(一个字节)转换为整数,然后使用 chr()
将其转换为字符:
假设字符串中编码的值是 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)你好
如果您想将其保留为原始编码,则无需进一步解码.通常,您会将传入的字符串转换为 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))返回 byte_string.decode(编码)
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"
into a UTF-8 string value.
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?
So how could I convert a binary string into a text string?
EDIT: 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
It looks like you are trying to decode ASCII characters from a binary string representation (bit string) of each character.
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
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
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屋!