How to display a byte array as hex values

前端 未结 5 2282
清酒与你
清酒与你 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: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
    

提交回复
热议问题