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

前端 未结 9 2033
一生所求
一生所求 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:54

    Use the binascii module:

    >>> import binascii
    >>> binascii.hexlify('foo'.encode('utf8'))
    b'666f6f'
    >>> binascii.unhexlify(_).decode('utf8')
    'foo'
    

    See this answer: Python 3.1.1 string to hex

提交回复
热议问题