为了账号安全,请及时绑定邮箱和手机立即绑定

将十六进制转储转换为 ASCII

将十六进制转储转换为 ASCII

扬帆大鱼 2023-02-07 09:37:10
所以我想知道是否有可能使用 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 的输出“:
查看完整描述

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 ................


查看完整回答
反对 回复 2023-02-07
?
开满天机

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 |................|


查看完整回答
反对 回复 2023-02-07
  • 2 回答
  • 0 关注
  • 117 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信