将二进制转换为 ASCII,反之亦然 [英] Convert binary to ASCII and vice versa
问题描述
使用此代码获取字符串并将其转换为二进制:
bin(reduce(lambda x, y: 256*x+y, (ord(c) for c in 'hello'), 0))
这个输出:
0b110100001100101011011000110110001101111
如果我把它放到这个网站(在右手边site) 我收到了 hello
的消息.我想知道它使用什么方法.我知道我可以将二进制字符串拼接成 8 个,然后将其与 bin(ord(character))
或其他方式的相应值匹配.真的在寻找更简单的东西.
对于 Python 2 上 [ -~]
范围内的 ASCII 字符:
反过来:
<预><代码>>>>n = int('0b110100001100101011011000110110001101111', 2)>>>binascii.unhexlify('%x' % n)'你好'<小时>
在 Python 3.2+ 中:
<预><代码>>>>bin(int.from_bytes('hello'.encode(), 'big'))'0b110100001100101011011000110110001101111'反过来:
<预><代码>>>>n = int('0b110100001100101011011000110110001101111', 2)>>>n.to_bytes((n.bit_length() + 7)//8, 'big').decode()'你好'<小时>
在 Python 3 中支持所有 Unicode 字符:
def text_to_bits(text, encoding='utf-8', errors='surrogatepass'):位 = bin(int.from_bytes(text.encode(encoding, errors), 'big'))[2:]return bits.zfill(8 * ((len(bits) + 7)//8))def text_from_bits(bits, encoding='utf-8', errors='surrogatepass'):n = int(位,2)return n.to_bytes((n.bit_length() + 7)//8, 'big').decode(encoding, errors) 或 ' '
这是单源 Python 2/3 兼容版本:
导入二进制文件def text_to_bits(text, encoding='utf-8', errors='surrogatepass'):位 = bin(int(binascii.hexlify(text.encode(encoding, errors)), 16))[2:]return bits.zfill(8 * ((len(bits) + 7)//8))def text_from_bits(bits, encoding='utf-8', errors='surrogatepass'):n = int(位,2)返回 int2bytes(n).decode(encoding, errors)def int2bytes(i):hex_string = '%x' % in = len(hex_string)返回 binascii.unhexlify(hex_string.zfill(n + (n & 1)))
示例
<预><代码>>>>text_to_bits('你好')'0110100001100101011011000110110001101111'>>>text_from_bits('110100001100101011011000110110001101111') == u'hello'真的Using this code to take a string and convert it to binary:
bin(reduce(lambda x, y: 256*x+y, (ord(c) for c in 'hello'), 0))
this outputs:
0b110100001100101011011000110110001101111
Which, if I put it into this site (on the right hand site) I get my message of hello
back. I'm wondering what method it uses. I know I could splice apart the string of binary into 8's and then match it to the corresponding value to bin(ord(character))
or some other way. Really looking for something simpler.
For ASCII characters in the range [ -~]
on Python 2:
>>> import binascii
>>> bin(int(binascii.hexlify('hello'), 16))
'0b110100001100101011011000110110001101111'
In reverse:
>>> n = int('0b110100001100101011011000110110001101111', 2)
>>> binascii.unhexlify('%x' % n)
'hello'
In Python 3.2+:
>>> bin(int.from_bytes('hello'.encode(), 'big'))
'0b110100001100101011011000110110001101111'
In reverse:
>>> n = int('0b110100001100101011011000110110001101111', 2)
>>> n.to_bytes((n.bit_length() + 7) // 8, 'big').decode()
'hello'
To support all Unicode characters in Python 3:
def text_to_bits(text, encoding='utf-8', errors='surrogatepass'):
bits = bin(int.from_bytes(text.encode(encoding, errors), 'big'))[2:]
return bits.zfill(8 * ((len(bits) + 7) // 8))
def text_from_bits(bits, encoding='utf-8', errors='surrogatepass'):
n = int(bits, 2)
return n.to_bytes((n.bit_length() + 7) // 8, 'big').decode(encoding, errors) or ' '
Here's single-source Python 2/3 compatible version:
import binascii
def text_to_bits(text, encoding='utf-8', errors='surrogatepass'):
bits = bin(int(binascii.hexlify(text.encode(encoding, errors)), 16))[2:]
return bits.zfill(8 * ((len(bits) + 7) // 8))
def text_from_bits(bits, encoding='utf-8', errors='surrogatepass'):
n = int(bits, 2)
return int2bytes(n).decode(encoding, errors)
def int2bytes(i):
hex_string = '%x' % i
n = len(hex_string)
return binascii.unhexlify(hex_string.zfill(n + (n & 1)))
Example
>>> text_to_bits('hello')
'0110100001100101011011000110110001101111'
>>> text_from_bits('110100001100101011011000110110001101111') == u'hello'
True
这篇关于将二进制转换为 ASCII,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!