Hexdump and Hexdiff

Converting bytes to characters

source

hexdump_line

 hexdump_line (chunk, bits, ascii=None, width=128, highlight=None)
print(hexdump_line(range(16), 8))
00 01 02 03 04 05 06 07  08 09 0A 0B 0C 0D 0E 0F  |................|
print(hexdump_line(range(0x12345678, 0x12345678+4), 32, ascii=True))
12345678 12345679  1234567A 1234567B  |.4Vx.4Vy.4Vz.4V{|
print(hexdump_line(range(0x12345678, 0x12345678+4), 32, ascii=True, highlight=["red", "green", "BLACK", (None, "blue")]))
12345678 12345679  1234567A 1234567B  |.4Vx.4Vy.4Vz.4V{|
print(hexdump_line(range(32), 8, ascii=True, width=128))
00 01 02 03 04 05 06 07  08 09 0A 0B 0C 0D 0E 0F  10 11 12 13 14 15 16 17  18 19 1A 1B 1C 1D 1E 1F  |................................|

source

hexdump

 hexdump (data, bits, highlight=None, ascii=None, width=128)
print(hexdump(list(range(64)), 8, highlight=["BLACK", None, "red", "RED"]))
00: 00 01 02 03 04 05 06 07  08 09 0A 0B 0C 0D 0E 0F  |................|
10: 10 11 12 13 14 15 16 17  18 19 1A 1B 1C 1D 1E 1F  |................|
20: 20 21 22 23 24 25 26 27  28 29 2A 2B 2C 2D 2E 2F  | !"#$%&'()*+,-./|
30: 30 31 32 33 34 35 36 37  38 39 3A 3B 3C 3D 3E 3F  |0123456789:;<=>?|
print(hexdump(range(48, 64), 32, ascii=True, highlight=["BLACK", None, "red", "RED"]*2))
00: 00000030 00000031  00000032 00000033  |...0...1...2...3|
10: 00000034 00000035  00000036 00000037  |...4...5...6...7|
20: 00000038 00000039  0000003A 0000003B  |...8...9...:...;|
30: 0000003C 0000003D  0000003E 0000003F  |...<...=...>...?|
data = list(range(8)) * 8
print(hexdump(data, bits=8, highlight=[ "BLACK" if d==0 else None for d in data] ))
00: 00 01 02 03 04 05 06 07  00 01 02 03 04 05 06 07  |................|
10: 00 01 02 03 04 05 06 07  00 01 02 03 04 05 06 07  |................|
20: 00 01 02 03 04 05 06 07  00 01 02 03 04 05 06 07  |................|
30: 00 01 02 03 04 05 06 07  00 01 02 03 04 05 06 07  |................|

source

hexdiff

 hexdiff (data1, data2, bits, ascii=None, width=128)

*Compare two arrays side by side in hexdump format, highlighting differences in CYAN color.

Args: data1: First array to compare data2: Second array to compare bits: Number of bits per element (8, 16, 32, etc.) ascii: Whether to show ASCII representation (default: True for 8-bit, False otherwise) width: Width of each line in bits (default: 128)

Returns: String with the side-by-side hexdump comparison*

data1 = list(range(32)) + [32, 33, 34, 35] + list(range(40, 64))
data2 = list(range(32)) + [1, 2, 3, 4]     + list(range(40, 60)) + [1,2,3,4,64, 65]

print(hexdiff(data1, data2, 8))
00: 00 01 02 03 04 05 06 07  08 09 0A 0B 0C 0D 0E 0F  |................|    00 01 02 03 04 05 06 07  08 09 0A 0B 0C 0D 0E 0F  |................|
10: 10 11 12 13 14 15 16 17  18 19 1A 1B 1C 1D 1E 1F  |................|    10 11 12 13 14 15 16 17  18 19 1A 1B 1C 1D 1E 1F  |................|
20: 20 21 22 23 28 29 2A 2B  2C 2D 2E 2F 30 31 32 33  | !"#()*+,-./0123| => 01 02 03 04 28 29 2A 2B  2C 2D 2E 2F 30 31 32 33  |....()*+,-./0123|
30: 34 35 36 37 38 39 3A 3B  3C 3D 3E 3F              |456789:;<=>?|     => 34 35 36 37 38 39 3A 3B  01 02 03 04 40 41        |456789:;....@A|  

source

HexIntArray

 HexIntArray (data=None, bits=32, signed=False)

Initialize self. See help(type(self)) for accurate signature.

a = HexIntArray([0x12345678, 0x9ABCDEF0, 0, 1], bits=32, signed=False)
print(a.hexdump(ascii=True))
0: 12345678 9ABCDEF0  00000000 00000001  |.4Vx............|
a = (ctypes.c_uint16 * 64)(*range(64))
print(HexIntArray.from_ctypes(a).hexdump(ascii=True))
00: 0000 0001 0002 0003  0004 0005 0006 0007  |................|
10: 0008 0009 000A 000B  000C 000D 000E 000F  |................|
20: 0010 0011 0012 0013  0014 0015 0016 0017  |................|
30: 0018 0019 001A 001B  001C 001D 001E 001F  |................|
40: 0020 0021 0022 0023  0024 0025 0026 0027  |. .!.".#.$.%.&.'|
50: 0028 0029 002A 002B  002C 002D 002E 002F  |.(.).*.+.,.-.../|
60: 0030 0031 0032 0033  0034 0035 0036 0037  |.0.1.2.3.4.5.6.7|
70: 0038 0039 003A 003B  003C 003D 003E 003F  |.8.9.:.;.<.=.>.?|
a = (ctypes.c_uint8 * 64)(*range(32, 96))
print(ah:=HexIntArray.from_ctypes(a).hexdump())
00: 20 21 22 23 24 25 26 27  28 29 2A 2B 2C 2D 2E 2F  | !"#$%&'()*+,-./|
10: 30 31 32 33 34 35 36 37  38 39 3A 3B 3C 3D 3E 3F  |0123456789:;<=>?|
20: 40 41 42 43 44 45 46 47  48 49 4A 4B 4C 4D 4E 4F  |@ABCDEFGHIJKLMNO|
30: 50 51 52 53 54 55 56 57  58 59 5A 5B 5C 5D 5E 5F  |PQRSTUVWXYZ[\]^_|
b = (ctypes.c_uint8 * 60).from_buffer_copy(a)
b[17:24] = (ctypes.c_uint8 * 7)(0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77)
print(bh:=HexIntArray.from_ctypes(b).hexdump())
00: 20 21 22 23 24 25 26 27  28 29 2A 2B 2C 2D 2E 2F  | !"#$%&'()*+,-./|
10: 30 11 22 33 44 55 66 77  38 39 3A 3B 3C 3D 3E 3F  |0."3DUfw89:;<=>?|
20: 40 41 42 43 44 45 46 47  48 49 4A 4B 4C 4D 4E 4F  |@ABCDEFGHIJKLMNO|
30: 50 51 52 53 54 55 56 57  58 59 5A 5B              |PQRSTUVWXYZ[|    
ah = HexIntArray.from_ctypes(a)
bh = HexIntArray.from_ctypes(b)
print(ah.diff(bh))
00: 20 21 22 23 24 25 26 27  28 29 2A 2B 2C 2D 2E 2F  | !"#$%&'()*+,-./|    20 21 22 23 24 25 26 27  28 29 2A 2B 2C 2D 2E 2F  | !"#$%&'()*+,-./|
10: 30 31 32 33 34 35 36 37  38 39 3A 3B 3C 3D 3E 3F  |0123456789:;<=>?| => 30 11 22 33 44 55 66 77  38 39 3A 3B 3C 3D 3E 3F  |0."3DUfw89:;<=>?|
20: 40 41 42 43 44 45 46 47  48 49 4A 4B 4C 4D 4E 4F  |@ABCDEFGHIJKLMNO|    40 41 42 43 44 45 46 47  48 49 4A 4B 4C 4D 4E 4F  |@ABCDEFGHIJKLMNO|
30: 50 51 52 53 54 55 56 57  58 59 5A 5B 5C 5D 5E 5F  |PQRSTUVWXYZ[\]^_| => 50 51 52 53 54 55 56 57  58 59 5A 5B              |PQRSTUVWXYZ[|