myhexdump 函数详解
📜 myhexdump.py 的主函数:把字节转成 hex+ASCII 的可读格式。
📎 源码在线查看:
myhexdump.py#L297-L330(hexdump)
函数签名
python
def myhexdump(bytes, offset=0, *, begin=0, size=0, ...)输出格式
0000 47 45 54 20 2f 61 70 69 2f 75 73 65 72 20 48 54 GET /api/user HT
0010 54 50 2f 31 2e 31 0d 0a 48 6f 73 74 3a 20 ... TP/1.1..Host: ...- 左:偏移(4位 hex)
- 中:字节十六进制(空格分隔,每 16 字节一行)
- 右:ASCII(不可打印显示
.)
核心逻辑
python
for i in range(0, len(bytes), 16):
chunk = bytes[i:i+16]
hex_part = " ".join(f"{b:02x}" for b in chunk)
ascii_part = "".join(chr(b) if 32 <= b < 127 else "." for b in chunk)
print(f"{i+offset:04x} {hex_part:<48} {ascii_part}")参数
| 参数 | 作用 |
|---|---|
bytes | 输入字节 |
offset | 起始偏移显示值 |
begin | 从字节的哪个位置开始 |
size | 只显示 size 字节 |
r0capture 的使用
python
# ssl_log 里
print(myhexdump(data))把抓到的明文以 hex+ASCII 显示,既能看二进制结构又能看可读文本。
Py2/Py3 兼容
myhexdump 内联了兼容代码,处理 Py2 的 str/Py3 的 bytes 差异。详见 py2-py3-compat。