简体   繁体   中英

Python UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8c in position 2: invalid start byte

anyone know how do i decode my Hex value on Python version 3.6.0 ?

i was using x.decode("hex") but since python update it doesn't work anymore.

here is my hex value:

 01008C647620302E31302E372070762039393130333120736E20333137337C6661336232653863206D7A20313778313720736B6620343235357C34376330643162302073706620333237397C363236373361376520627066203332363137307C61653138366364642073746620323538397C623634383035633220616D66203335333230357C633736333133626200 

In Python 3.6 there is no need to decode strings (of type str ), since they are already utf-8. Further, the normal decode function, allows only "standard" string decodings. That is, this function cannot decode hex.

Instead, this "special" decoding functionality is moved to codecs.decode . Thus, you want to rewrite your code as:

import codecs
x = "01008C647620302E31302E372070762039393130333120736E20333137337C6661336232653863206D7A20313778313720736B6620343235357C34376330643162302073706620333237397C363236373361376520627066203332363137307C61653138366364642073746620323538397C623634383035633220616D66203335333230357C633736333133626200"
codecs.decode(x, 'hex')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM