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

前端 未结 9 1919
一生所求
一生所求 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:34
    import codecs
    codecs.getencoder('hex_codec')(b'foo')[0]
    

    works in Python 3.3 (so "hex_codec" instead of "hex").

    0 讨论(0)
  • 2020-11-22 14:39

    Since Python 3.5 this is finally no longer awkward:

    >>> b'\xde\xad\xbe\xef'.hex()
    'deadbeef'
    

    and reverse:

    >>> bytes.fromhex('deadbeef')
    b'\xde\xad\xbe\xef'
    

    works also with the mutable bytearray type.

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

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-22 14:44

    Python has bytes-to-bytes standard codecs that perform convenient transformations like quoted-printable (fits into 7bits ascii), base64 (fits into alphanumerics), hex escaping, gzip and bz2 compression. In Python 2, you could do:

    b'foo'.encode('hex')
    

    In Python 3, str.encode / bytes.decode are strictly for bytes<->str conversions. Instead, you can do this, which works across Python 2 and Python 3 (s/encode/decode/g for the inverse):

    import codecs
    codecs.getencoder('hex')(b'foo')[0]
    

    Starting with Python 3.4, there is a less awkward option:

    codecs.encode(b'foo', 'hex')
    

    These misc codecs are also accessible inside their own modules (base64, zlib, bz2, uu, quopri, binascii); the API is less consistent, but for compression codecs it offers more control.

    0 讨论(0)
  • 2020-11-22 14:47

    OK, the following answer is slightly beyond-scope if you only care about Python 3, but this question is the first Google hit even if you don't specify the Python version, so here's a way that works on both Python 2 and Python 3.

    I'm also interpreting the question to be about converting bytes to the str type: that is, bytes-y on Python 2, and Unicode-y on Python 3.

    Given that, the best approach I know is:

    import six
    
    bytes_to_hex_str = lambda b: ' '.join('%02x' % i for i in six.iterbytes(b))
    

    The following assertion will be true for either Python 2 or Python 3, assuming you haven't activated the unicode_literals future in Python 2:

    assert bytes_to_hex_str(b'jkl') == '6a 6b 6c'
    

    (Or you can use ''.join() to omit the space between the bytes, etc.)

    0 讨论(0)
提交回复
热议问题