将 Python 的 binascii.crc_hqx() 转换回 ascii
Convert Python's binascii.crc_hqx() back to ascii
我正在使用标准 Python3 库 binascii
, and specifically the crc_hqx()
函数
binascii.crc_hqx(data, value)
Compute a 16-bit CRC value of data, starting with value as the initial CRC, and return the result. This uses the CRC-CCITT polynomial x16 + x12 + x5 + 1, often represented as 0x1021. This CRC is used in the binhex4 format.
我可以使用此代码转换为 CRC:
import binascii
t = 'abcd'
z = binascii.crc_hqx(t.encode('ascii'), 0)
print(t,z)
正如预期的那样,它打印了行
abcd 43062
但是我如何转换回 ASCII?
我尝试了 a2b_hqx()
函数的变体
binascii.a2b_hqx(string)
Convert binhex4 formatted ASCII data to binary, without doing RLE-decompression. The string should contain a complete number of binary bytes, or (in case of the last portion of the binhex4 data) have the remaining bits zero.
最简单的版本是:
y = binascii.a2b_hqx(str(z))
但我也尝试过 bytearray()
and str.encode()
的变体,等等
对于此代码:
import binascii
t = 'abcd'
z = binascii.crc_hqx(t.encode('ascii'), 0)
print(t,z)
y = binascii.a2b_hqx(str(z))
回溯:
abcd 43062
Traceback (most recent call last):
File "test.py", line 5, in <module>
y = binascii.a2b_hqx(str(z))
binascii.Incomplete: String has incomplete number of bytes
并使用此代码:
y = binascii.a2b_hqx(bytearray(z))
这个回溯:
binascii.Error: Illegal char
生成的是校验和,无法转换回 ascii。
我正在使用标准 Python3 库 binascii
, and specifically the crc_hqx()
函数
binascii.crc_hqx(data, value)
Compute a 16-bit CRC value of data, starting with value as the initial CRC, and return the result. This uses the CRC-CCITT polynomial x16 + x12 + x5 + 1, often represented as 0x1021. This CRC is used in the binhex4 format.
我可以使用此代码转换为 CRC:
import binascii
t = 'abcd'
z = binascii.crc_hqx(t.encode('ascii'), 0)
print(t,z)
正如预期的那样,它打印了行
abcd 43062
但是我如何转换回 ASCII?
我尝试了 a2b_hqx()
函数的变体
binascii.a2b_hqx(string)
Convert binhex4 formatted ASCII data to binary, without doing RLE-decompression. The string should contain a complete number of binary bytes, or (in case of the last portion of the binhex4 data) have the remaining bits zero.
最简单的版本是:
y = binascii.a2b_hqx(str(z))
但我也尝试过 bytearray()
and str.encode()
的变体,等等
对于此代码:
import binascii
t = 'abcd'
z = binascii.crc_hqx(t.encode('ascii'), 0)
print(t,z)
y = binascii.a2b_hqx(str(z))
回溯:
abcd 43062
Traceback (most recent call last):
File "test.py", line 5, in <module>
y = binascii.a2b_hqx(str(z))
binascii.Incomplete: String has incomplete number of bytes
并使用此代码:
y = binascii.a2b_hqx(bytearray(z))
这个回溯:
binascii.Error: Illegal char
生成的是校验和,无法转换回 ascii。