Search code examples
pythonpython-3.xasciixxd

Convert Hex dump to ASCII


so i'm wondering if it's possible, using Bash or Python, to convert the following hex code into it's ascii output like it would have in xxd. As i've used OCR to get the text from the following image.

enter image description here Is there a way using bash or python that i can convert the following hex dump into ascii characters?

(the hex dump, is only the hex part without the 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 

With the hopeful output of something approximating the following (except in text form not an image), that is to say, the output of hexdump -C": enter image description here


Solution

  • I hope I understood your question well: you want to print ASCII characters from your hex dump that resembles the output of 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)))
    

    Prints:

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