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

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

    New in python 3.8, you can pass a delimiter argument to the hex function, as in this example

    >>> value = b'\xf0\xf1\xf2'
    >>> value.hex('-')
    'f0-f1-f2'
    >>> value.hex('_', 2)
    'f0_f1f2'
    >>> b'UUDDLRLRAB'.hex(' ', -4)
    '55554444 4c524c52 4142'
    

    https://docs.python.org/3/library/stdtypes.html#bytes.hex

提交回复
热议问题