Skip to content

Instantly share code, notes, and snippets.

@NorimasaNabeta
Created May 29, 2014 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NorimasaNabeta/f42365573bdaa69e3a39 to your computer and use it in GitHub Desktop.
Save NorimasaNabeta/f42365573bdaa69e3a39 to your computer and use it in GitHub Desktop.
hexdump
# -*- mode: python; coding: utf-8 -*-
#
# Time-stamp: <Thu May 29 21:49:30 東京 (標準時) 2014>
#
#
import os
import sys
import struct
import string
import binascii
# hexdump
#
#
def hexdump( dat, offset, size ):
def printable( d ):
c = chr( struct.unpack("B",d)[0])
if c in string.digits or c in string.ascii_letters or c in string.punctuation :
return c
return "."
def printdump( address, hexprint, asciiprint ):
print "%(addr)08x : %(tmp)s : %(asc)s" % {
'addr': (address & 0xfffffff0), 'tmp': hexprint, 'asc': asciiprint }
return
total_size = len(dat)
if offset + size > total_size:
size + total_size - offset
addr = offset & 0xfffffff0
pads = offset & 0x0000000f
tmp = ""
asc = ""
for idx in range(0,pads):
tmp = tmp + "-- "
asc = asc + "-"
addr = addr + 1
for idx in range(0,size):
tmp = tmp + binascii.hexlify(dat[offset+idx]) +" "
asc = asc + printable(dat[offset+idx])
if addr % 16 == 15:
printdump( addr, tmp, asc )
tmp = ""
asc = ""
addr = addr + 1
pads = (16 - (addr & 0x0000000f)) % 16
if pads > 0:
for idx in range(0,pads):
tmp = tmp + "-- "
asc = asc + "-"
addr = addr + 1
if tmp != "":
printdump( addr, tmp, asc )
return
#
#
#
if __name__ == '__main__':
print "Hi there", sys.argv[1]
f = open(sys.argv[1], "rb")
try:
d = f.read()
finally:
f.close()
print "test-1 18 size30"
hexdump(d, 20, 30)
print "test-2 0 size 32"
hexdump(d, 0, 32)
@NorimasaNabeta
Copy link
Author

こんな感じの16進ダンプが欲しくてつくる

python hexdump.py tmp.mp3
Hi there tmp.mp3
test-1 18 size30
00000010 : -- -- -- -- 79 00 45 35 5a 54 00 67 93 28 20 39 : ----y.E5ZT.g.(.9
00000020 : a5 8e 26 11 03 be 18 4a c1 c3 3a 8e 05 84 f5 bc : ..&....J..:.....
00000040 : 40 50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- : @P--------------
test-2 0 size 32
00000000 : ff fb 44 c0 00 00 05 a4 0f 1b 20 98 40 41 3c 27 : ..D.........@A<'
00000010 : e8 34 61 89 79 00 45 35 5a 54 00 67 93 28 20 39 : .4a.y.E5ZT.g.(.9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment