我正在尝试将 ASCII 字符串来回转换为其二进制表示,如下所示。
s=chr(0)*15 + chr(0x01)
bst = bin(int(binascii.hexlify(s), 16))
n = int(bst, 2)
binascii.unhexlify('%x' % n)
但是,最后我收到以下错误,这对我来说没有多大意义。
1 binascii.unhexlify('%x' % n)
TypeError:奇数长度的字符串
有什么问题,我该如何解决?
我正在尝试将 ASCII 字符串来回转换为其二进制表示,如下所示。
s=chr(0)*15 + chr(0x01)
bst = bin(int(binascii.hexlify(s), 16))
n = int(bst, 2)
binascii.unhexlify('%x' % n)
但是,最后我收到以下错误,这对我来说没有多大意义。
1 binascii.unhexlify('%x' % n)
TypeError:奇数长度的字符串
有什么问题,我该如何解决?
使用 python 控制台:
>>> help(binascii.unhexlify)
unhexlify(...)
a2b_hex(hexstr) -> s; Binary data of hexadecimal representation.
hexstr must contain an even number of hex digits (upper or lower case).
This function is also available as "unhexlify()"
所以错误是一致的。你需要做的是填充'0'
有一个偶数:
>>> binascii.unhexlify('0%x' % n)
'\x01'
n = int(wer, 2)
qqwslenf=len(wer)
qqwslenf=(qqwslenf/8)*2
qqwslenf=str(qqwslenf)
qqwslenf="%0"+qqwslenf+"x"
jlz=binascii.unhexlify(qqwslenf % n)
#we convert from binary to ASCII exactly size of need by half bytes.