How to display a byte array as hex values

前端 未结 5 1935
清酒与你
清酒与你 2020-12-20 15:59
>>> struct.pack(\'2I\',12, 30)
b\'\\x0c\\x00\\x00\\x00\\x1e\\x00\\x00\\x00\'    
>>> struct.pack(\'2I\',12, 31)
b\'\\x0c\\x00\\x00\\x00\\x1f\\x00\\         


        
相关标签:
5条回答
  • 2020-12-20 16:32

    Try binascii.hexlify:

    >>> import binascii
    >>> import struct
    >>> binascii.hexlify(struct.pack('2I',12,30))
    b'0c0000001e000000'
    >>> binascii.hexlify(struct.pack('2I',12,31))
    b'0c0000001f000000'
    >>> binascii.hexlify(struct.pack('2I',12,32))
    b'0c00000020000000'
    >>> binascii.hexlify(struct.pack('2I',12,33))
    b'0c00000021000000'
    

    Or if you want spaces to make it more readable:

    >>> ' '.join(format(n,'02X') for n in struct.pack('2I',12,33))
    '0C 00 00 00 21 00 00 00'
    

    Python 3.6+, using f-strings:

    >>> ' '.join(f'{n:02X}' for n in struct.pack('2I',12,33))
    '0C 00 00 00 21 00 00 00'
    
    0 讨论(0)
  • 2020-12-20 16:35

    There is an option to convert byte array to hex string with encode. It works for any Python from Python 2.4:

    Python 2.7.12 (default, Oct 10 2016, 12:50:22)
    >>> import struct
    >>> struct.pack('2I',12, 30).encode('hex')
    '0c0000001e000000'
    >>> struct.pack('2I',12, 31).encode('hex')
    '0c0000001f000000'
    >>> struct.pack('2I',12, 32).encode('hex')
    '0c00000020000000'
    >>> struct.pack('2I',12, 33).encode('hex')
    '0c00000021000000'
    
    0 讨论(0)
  • 2020-12-20 16:37

    In Python 3.7 bytes objects don't have an encode() method. The following code doesn't work anymore.

    import struct
    
    hex_str = struct.pack('2I',12, 30).encode('hex')
    

    Instead of encode(), Python 3.7 code should use the hex() method, introduced in Python 3.5.

    import struct
    
    # hex_str will contain the '0c0000001e000000' string.
    hex_str = struct.pack('2I',12, 30).hex()
    
    0 讨论(0)
  • 2020-12-20 16:43

    How about his?

    >>> data = struct.pack('2I',12, 30)
    >>> [hex(ord(c)) for c in data]
    ['0xc', '0x0', '0x0', '0x0', '0x1e', '0x0', '0x0', '0x0']
    

    The expression [item for item in sequence] is a so called list comprehension. It's basically a very compact way of writing a simple for loop, and creating a list from the result.

    The ord() builtin function takes a string, and turns it into an integer that's its corresponding unicode code point (for characters in the ASCII character set that's the same as their value in the ASCII table).

    Its counterpart, chr() for 8bit strings or unichr() for unicode objects do the opposite.

    The hex() builtin then simply converts the integers into their hex representation.


    As pointed out by @TimPeters, in Python 3 you would need to lose the ord(), because iterating over a bytes object will (already) yield integers:

    Python 3.4.0a3 (default, Nov  8 2013, 18:33:56)
    >>> import struct
    >>> data = struct.pack('2I',12, 30)
    >>> type(data)
    <class 'bytes'>
    >>> type(data[1])
    <class 'int'>
    >>>
    >>> [hex(i) for i in data]
    ['0xc', '0x0', '0x0', '0x0', '0x1e', '0x0', '0x0', '0x0']
    
    0 讨论(0)
  • 2020-12-20 16:46

    You have to reformat it yourself if you want \x escapes everywhere; e.g.,

    >>> import struct
    >>> r = struct.pack('2I',12, 33)
    >>> r
    b'\x0c\x00\x00\x00!\x00\x00\x00'
    >>> list(r)
    [12, 0, 0, 0, 33, 0, 0, 0]
    >>> print("".join("\\x%02x" % i for i in r))
    \x0c\x00\x00\x00\x21\x00\x00\x00
    
    0 讨论(0)
提交回复
热议问题