二进制字段的描述是:
来电号码,用压缩的BCD码表示,多余的位用0xF"填充
Caller number, expressed with compressed BCD code, and the surplus bits are filled with "0xF"
我尝试使用结构格式 '16c'
进行打印,我得到:('3', 'x00', 'x02', 'x05', 'x15'、'x13'、'G'、'O'、'xff'、'xff'、'xff'、'xff'、'xff'、'xff'、'xff', 'xff')
如果我使用 '16b'
我得到 (51, 0, 2, 5, 21, 19, 71, 79, -1, -1、-1、-1、-1、-1、-1、-1)
.而且不正确,我应该得到电话号码,上面的号码是无效的.
I have tried to print with struct format '16c'
and I get: ('3', 'x00', 'x02', 'x05', 'x15', 'x13', 'G', 'O', 'xff', 'xff', 'xff', 'xff', 'xff', 'xff', 'xff', 'xff')
and if I use '16b'
i get (51, 0, 2, 5, 21, 19, 71, 79, -1, -1, -1, -1, -1, -1, -1, -1)
. And it is not correct, I should get phone number, and numbers above are invalid.
print struct.unpack_from('>16b', str(data.read()),offset=46)
上面是无效的代码,我得到了无效的数字.我应该用什么格式解压那个 16 字节的字段以及如何转换 BCD 码?
Above is code that didn't work and I get invalid numbers. With what format should I unpack that 16 byte field and how to convert BCD code ?
BCD 码使用每个数字 4 位,通常只编码数字 0 - 9.所以序列中的每个字节包含 2 个数字,每 4 位 1 个信息.
BCD codes work with 4 bits per number, and normally encode only the digits 0 - 9. So each byte in your sequence contains 2 numbers, 1 per 4 bits of information.
以下方法使用生成器来生成这些数字;我假设 0xF 值意味着没有更多的数字可以跟随:
The following method uses a generator to produce those digits; I am assuming that a 0xF value means there are no more digits to follow:
def bcdDigits(chars):
for char in chars:
char = ord(char)
for val in (char >> 4, char & 0xF):
if val == 0xF:
return
yield val
这里我使用 右移运算符 向左移动- 最右边 4 位,和一个 按位与 来选择最右边的 4 位.
Here I use a right-shift operator to move the left-most 4 bits to the right, and a bitwise AND to select just the right-most 4 bits.
演示:
>>> characters = ('3', 'x00', 'x02', 'x05', 'x15', 'x13', 'G', 'O', 'xff', 'xff', 'xff', 'xff', 'xff', 'xff', 'xff', 'xff')
>>> list(bcdDigits(characters))
[3, 3, 0, 0, 0, 2, 0, 5, 1, 5, 1, 3, 4, 7, 4]
该方法适用于 c
输出;如果直接传递整数,则可以跳过方法中的 ord
调用(但请改用 B
无符号变体).或者,您可以直接从文件中读取这 16 个字节,然后直接将此函数应用于这些字节,而无需使用 struct.
The method works with the c
output; you can skip the ord
call in the method if you pass integers directly (but use the B
unsigned variant instead). Alternatively, you could just read those 16 bytes straight from your file and apply this function to those bytes directly without using struct.
这篇关于Python,如何解码二进制编码的十进制(BCD)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!