2 回答
TA贡献1829条经验 获得超4个赞
我希望我能很好地理解您的问题:您想从类似于以下输出的十六进制转储中打印 ASCII 字符hexdump:
txt = '''
00000690 52 4D 41 50 00 00 01 00 08 00 00 00 08 00 00 00
000006A0 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
000006B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
000006C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
000006D0 00 00 00 00 00 00 00 00 30 E0 01 04 90 9B 00 01
000006E0 B9 4D E9 46 5B 43 00 10 B2 BA FB 46 BA 24 13 16
000006F0 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00
00000700 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 '''
for line in txt.splitlines():
if not line.strip():
continue
address, *nums = line.split()
# printable ascii characters are from 32 to 127, else print a dot '.':
print('{} {} {}'.format(address, ' '.join(nums), ''.join(chr(int(n, 16)) if 32 <= int(n, 16) <= 127 else '.' for n in nums)))
印刷:
00000690 52 4D 41 50 00 00 01 00 08 00 00 00 08 00 00 00 RMAP............
000006A0 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000006B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000006C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000006D0 00 00 00 00 00 00 00 00 30 E0 01 04 90 9B 00 01 ........0.......
000006E0 B9 4D E9 46 5B 43 00 10 B2 BA FB 46 BA 24 13 16 .M.F[C.....F.$..
000006F0 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 ................
00000700 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
TA贡献1786条经验 获得超13个赞
假设两个连续的十六进制字节之间以及偏移量和第一个十六进制字节之间恰好有一个空格字符,在输入行中,bash 实现将是:
$ cat hexdump-C
#!/bin/bash
while read hex; do
# Delete offset
ascii=" ${hex#* }"
# Replace non-printable bytes with a dot
ascii=${ascii// [0189ABCDEF]?/.}
# Replace space characters with \x
ascii=${ascii// /\\x}
# printf replaces the \xhh sequences with the corresponding characters
printf "$hex |$ascii|\n"
done
--
$ cat data
00000690 52 4D 41 50 00 00 01 00 08 00 00 00 08 00 00 00
000006A0 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
000006B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
000006C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
000006D0 00 00 00 00 00 00 00 00 30 E0 01 04 90 9B 00 01
000006E0 B9 4D E9 46 5B 43 00 10 B2 BA FB 46 BA 24 13 16
000006F0 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00
00000700 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
--
$ ./hexdump-C < data
00000690 52 4D 41 50 00 00 01 00 08 00 00 00 08 00 00 00 |RMAP............|
000006A0 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000006B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000006C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000006D0 00 00 00 00 00 00 00 00 30 E0 01 04 90 9B 00 01 |........0.......|
000006E0 B9 4D E9 46 5B 43 00 10 B2 BA FB 46 BA 24 13 16 |.M.F[C.....F.$..|
000006F0 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 |................|
00000700 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
添加回答
举报