2017-07-14 63 views
0

鑑於字面b'a16568656C6c6f65776f726c64'我想它作爲第一個數字被讀取例如是「0xa1」,第二個是「0x65」,等...解釋一個字節數組字面爲十六進制字節數組字面

這是的{"hello": "world"}一個CBOR編碼,但下面的程序不會產生預期的輸出:

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

打印1.
現在它解碼,就好像「一」是字的第一個字符。

+0

爲什麼不初始化'cipher'作爲一個字符串,而不是一個'bytes'對象? –

回答

0

找到我一直在尋找幫手:binascii.a2b_hex

import cbor2 
import binascii 
cipher=b'a16568656C6c6f65776f726c64' 
plain=cbor2.loads(binascii.a2b_hex(cipher)) 
print(plain) 
相關問題