小编典典

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

python

使用以下代码获取字符串并将其转换为二进制:

bin(reduce(lambda x, y: 256*x+y, (ord(c) for c in 'hello'), 0))

输出:

0b110100001100101011011000110110001101111

如果我将其放到该站点(位于右侧站点)中,则会得到hello回信。我想知道它使用什么方法。我知道我可以将二进制字符串拼接成8,然后将其与相应的值进行匹配bin(ord(character))或以其他方式进行匹配。真正在寻找更简单的东西。


阅读 1482

收藏
2020-02-14

共1个答案

小编典典

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

>>> import binascii
>>> bin(int(binascii.hexlify('hello'), 16))
'0b110100001100101011011000110110001101111'

相反:

>>> n = int('0b110100001100101011011000110110001101111', 2)
>>> binascii.unhexlify('%x' % n)
'hello'

在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()
'hello'

要在Python中支持所有Unicode字符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 '\0'

这是与Python 2/3兼容的单源版本:

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

>>> text_to_bits('hello')
'0110100001100101011011000110110001101111'
>>> text_from_bits('110100001100101011011000110110001101111') == u'hello'
True
2020-02-14