Debian Codegraph @

doc/reveng/pinspect/utilities.py

raw
1 def hexDump(s: bytes, prefix: str = "", addr: int = 0) -> str:
2 """ Utility function to hex-dump binary data. """
3 N = len(s)
4 Nb = N//16
5 if (N%16): Nb += 1
6 res = ""
7 for j in range(Nb):
8 a,b = j*16, min((j+1)*16,N)
9
10 h = " ".join(map("{:02x}".format, s[a:b]))
11 h += " "*(16-(b-a))
12 t = ""
13 for i in range(a,b):
14 c = s[i]
15 if c>=0x20 and c<0x7f:
16 t += chr(c)
17 else:
18 t += "."
19 res += (prefix + "{:08X} ".format(addr+16*j) + h + " | " + t + "\n")
20 return res[:-1]