What's the correct way to convert bytes to a hex string in Python 3?

前端 未结 9 2032
一生所求
一生所求 2020-11-22 14:11

What\'s the correct way to convert bytes to a hex string in Python 3?

I see claims of a bytes.hex method, bytes.decode codecs, and have tri

9条回答
  •  一个人的身影
    2020-11-22 14:51

    If you want to convert b'\x61' to 97 or '0x61', you can try this:

    [python3.5]
    >>>from struct import *
    >>>temp=unpack('B',b'\x61')[0] ## convert bytes to unsigned int
    97
    >>>hex(temp) ##convert int to string which is hexadecimal expression
    '0x61'
    

    Reference:https://docs.python.org/3.5/library/struct.html

提交回复
热议问题