Created
December 20, 2022 02:34
-
-
Save SeeFlowerX/84f54579eaef607fa68f1aed12d23eef to your computer and use it in GitHub Desktop.
打印hex数据,简单优雅,适合接入unicorn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# from https://gist.github.com/NeatMonster/c06c61ba4114a2b31418a364341c26c0 | |
class hexdump: | |
def __init__(self, buf, off=0): | |
self.buf = buf | |
self.off = off | |
def __iter__(self): | |
last_bs, last_line = None, None | |
for i in range(0, len(self.buf), 16): | |
bs = bytearray(self.buf[i:i + 16]) | |
line = "{:08x} {:23} {:23} |{:16}|".format( | |
self.off + i, | |
" ".join(("{:02x}".format(x) for x in bs[:8])), | |
" ".join(("{:02x}".format(x) for x in bs[8:])), | |
"".join((chr(x) if 32 <= x < 127 else "." for x in bs)), | |
) | |
if bs == last_bs: | |
line = "*" | |
if bs != last_bs or line != last_line: | |
yield line | |
last_bs, last_line = bs, line | |
yield "{:08x}".format(self.off + len(self.buf)) | |
def __str__(self): | |
return "\n".join(self) | |
def __repr__(self): | |
return "\n".join(self) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment