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

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

    it can been used the format specifier %x02 that format and output a hex value. For example:

    >>> foo = b"tC\xfc}\x05i\x8d\x86\x05\xa5\xb4\xd3]Vd\x9cZ\x92~'6"
    >>> res = ""
    >>> for b in foo:
    ...     res += "%02x" % b
    ... 
    >>> print(res)
    7443fc7d05698d8605a5b4d35d56649c5a927e2736
    

提交回复
热议问题