将十六进制转储转换为 ASCII

Convert Hex dump to ASCII

所以我想知道是否可以使用 Bash 或 Python 将以下十六进制代码转换为它的 ascii 输出,就像在 xxd 中一样。因为我已经使用 OCR 从下图中获取文本。

有没有一种方法可以使用 bash 或 python 将以下十六进制转储转换为 ascii 字符?

(十六进制转储,只是没有 ascii 的十六进制部分)

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 的输出“:

希望我能很好地理解您的问题:您想从类似于 hexdump:

的输出的十六进制转储中打印 ASCII 字符
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 ................

假设两个连续的十六进制字节之间以及偏移量和第一个十六进制字节之间恰好有一个 space 字符,在输入行中,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 |................|