且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将二进制转换为 ASCII,反之亦然

更新时间:2022-12-07 17:23:29

对于 Python 2 上 [ -~] 范围内的 ASCII 字符:

>>>导入二进制文件>>>bin(int(binascii.hexlify('你好'), 16))'0b110100001100101011011000110110001101111'

反过来:

>>>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