Python, how to decode Binary coded decimal (BCD)

后端 未结 2 1193
醉话见心
醉话见心 2021-01-02 04:40

Description of the binary field is:

Caller number, expressed with compressed BCD code, and the surplus bits are filled with “0xF”

2条回答
  •  没有蜡笔的小新
    2021-01-02 04:54

    Python 3.x has this built-in for bytes objects.

    >>> c = b'3\x00\x02\x05\x15\x13GO\xff\xff\xff\xff\xff\xff\xff\xff'
    >>> c.hex()
    '330002051513474fffffffffffffffff'
    

    And back:

    >>> c = "9F1A020840"
    >>> bytes.fromhex(c)
    b'\x9f\x1a\x02\x08@'
    

提交回复
热议问题