fearanp
fearanp

Reputation: 13

Python Hexdump similar to Linux Hexdump

I have a 1.5MB binary file. I am using this HEXDUMP module in python. https://pypi.org/project/hexdump/ . When I dump the HEX from python the hex bytes are reversed i.e 1f03 080b etc. Is there a module in python that dumps the HEX same as linux ( cmd: hexdump filename )

Example of linux hex dump.

031f 0b08 0000 0567 00fd 0ff8 0004 0000
0000 0248 0001 06c8 00dc 0001 0004 0000

Python output

1F 03 08 0B 00 00 67 05  FD 00 F8 0F 04 00 00 00
00 00 48 02 01 00 C8 06  DC 00 01 00 04 00 00 00

thanks

Upvotes: 0

Views: 766

Answers (1)

Zibri
Zibri

Reputation: 9827

import os
import textwrap

def hdump(h):
  print(textwrap.fill(' '.join(list(('0'+hex(x)[2:])[-2:3] for x in h)),48))

hdump(os.urandom(256))

output:

50 18 6d 57 7c db 3e 85 e4 f9 3e f5 80 4b e4 bf
cd 66 e1 9f 90 34 cd 40 1a 79 76 d1 33 2a 7c c0
dc 73 1e 78 e2 69 5a eb b7 36 c7 76 08 e9 07 bd
ad 76 bf 44 52 82 c2 bf 0a 80 2e fc d8 f2 09 eb
d8 be 5f 10 cc 58 30 76 ff 83 18 c7 74 61 7c 5a
af 70 1f cc 29 d0 17 a4 94 14 d1 ef 99 a1 2f f3
ad 6a 58 3d c0 b2 6e 41 3c fe 8c 2f c3 cc 9b 2f
58 7e 13 eb ba 0f 61 58 80 b1 49 98 c6 f8 f4 01
a1 9f 43 73 ad be 26 8c ab 93 86 fd a0 90 d4 f1
a3 50 07 30 cf 08 2a 10 f4 81 70 29 7b 80 0e d4
3e 21 b2 d5 91 4d 7f 15 e6 ef f2 e4 dd 8d 74 86
47 ac 13 2d d4 cf 58 80 1b 8d 88 f1 a1 77 9b 8a
4e e5 1f 4c f9 92 70 83 8d c5 42 1a 52 aa f0 eb
10 b9 6c 4a 38 5d 30 7c ce 93 fb a7 d0 5d ab 22
cb 1e 1a 8e ce 08 5c 68 a4 fd 5c fd 00 52 c9 1c
47 11 6a 78 1b 55 07 33 0e be 85 1c 50 7d cb b0

Upvotes: 0

Related Questions