Search code examples
pythonjsoncharacter-encodingcbor

Interpret a byte array literal as hexadecimal byte array literal


Given the literal b'a16568656C6c6f65776f726c64' I would like it to be read such as the first digit is '0xa1', second is '0x65', etc...

This is a cbor encoding of {"hello": "world"} but the following program doesn't produce the expected output :

import cbor2
cipher=b'a16568656C6c6f65776f726c64'
plain=cbor2.loads(cipher)
print(plain)

print 1.
Right now it decodes as if 'a' is the first character of the literal.


Solution

  • Found the helper I was looking for : binascii.a2b_hex

    import cbor2
    import binascii
    cipher=b'a16568656C6c6f65776f726c64'
    plain=cbor2.loads(binascii.a2b_hex(cipher))
    print(plain)