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 ................
Answer from Andrej Kesely on Stack OverflowI 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 ................
Assuming there is exactly one space character between two consecutive hexadecimal bytes, and between the offset and the first hexadecimal byte, in the input lines, a bash implementation would be:
$ 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 "
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 |................|
[01/14/13] Challenge #117 [Easy] Hexdump to ASCII
Trying to find hex in bin file
Real Time Hexdump Output
XOR of Hex
hexdump -C does what you want.
# hexdump -C /etc/passwd
00000000 72 6f 6f 74 3a 78 3a 30 3a 30 3a 72 6f 6f 74 3a |root:x:0:0:root:|
00000010 2f 72 6f 6f 74 3a 2f 62 69 6e 2f 62 61 73 68 0a |/root:/bin/bash.|
00000020 64 61 65 6d 6f 6e 3a 78 3a 31 3a 31 3a 64 61 65 |daemon:x:1:1:dae|
00000030 6d 6f 6e 3a 2f 75 73 72 2f 73 62 69 6e 3a 2f 62 |mon:/usr/sbin:/b|
00000040 69 6e 2f 73 68 0a 62 69 6e 3a 78 3a 32 3a 32 3a |in/sh.bin:x:2:2:|
00000050 62 69 6e 3a 2f 62 69 6e 3a 2f 62 69 6e 2f 73 68 |bin:/bin:/bin/sh|
...
The vim editor usually (?) includes the tool xxd.
$ xxd `which xxd` | head -n 10
0000000: 7f45 4c46 0201 0100 0000 0000 0000 0000 .ELF............
0000010: 0200 3e00 0100 0000 400a 4000 0000 0000 ..>.....@.@.....
0000020: 4000 0000 0000 0000 f035 0000 0000 0000 @........5......
0000030: 0000 0000 4000 3800 0800 4000 1b00 1a00 ....@.8...@.....
0000040: 0600 0000 0500 0000 4000 0000 0000 0000 ........@.......
0000050: 4000 4000 0000 0000 4000 4000 0000 0000 @.@.....@.@.....
0000060: c001 0000 0000 0000 c001 0000 0000 0000 ................
0000070: 0800 0000 0000 0000 0300 0000 0400 0000 ................
0000080: 0002 0000 0000 0000 0002 4000 0000 0000 ..........@.....
0000090: 0002 4000 0000 0000 1c00 0000 0000 0000 ..@.............
(Easy): Hexdump to ASCII
Hexadecimal is a base-16 representation of a number. A single byte of information, as an unsigned integer, can have a value of 0 to 255 in decimal. This byte can be represented in hexadecimal, from a range of 0x0 to 0xFF in hexadecimal.
Your job is to open a given file (using the given file name) and print every byte's hexadecimal value.
Author: PoppySeedPlehzr
Formal Inputs & Outputs
Input Description
As a program command-line argument to the program, accept a valid file name.
Output Description
Print the given file's contents, where each byte of the file must be printed in hexadecimal form. Your program must print 16 bytes per line, where there is a space between each hexadecimal byte. Each line must start with the line number, starting from line 0, and must also count in hexadecimal.
Sample Inputs & Outputs
Sample Input
"MyFile.txt" (This file is an arbitrary file as an example)
Sample Output
00000000 37 7A BC AF 27 1C 00 03 38 67 83 24 70 00 00 00 00000001 00 00 00 00 49 00 00 00 00 00 00 00 64 FC 7F 06 00000002 00 28 12 BC 60 28 97 D5 68 12 59 8C 17 8F FE D8 00000003 0E 5D 2C 27 BC D1 87 F6 D2 BE 9B 92 90 E8 FD BA 00000004 A2 B8 A9 F4 BE A6 B8 53 10 E3 BD 60 05 2B 5C 95 00000005 C4 50 B4 FC 10 DE 58 80 0C F5 E1 C0 AC 36 30 74 00000006 82 8B 42 7A 06 A5 D0 0F C2 4F 7B 27 6C 5D 96 24 00000007 25 4F 3A 5D F4 B2 C0 DB 79 3C 86 48 AB 2D 57 11 00000008 53 27 50 FF 89 02 20 F6 31 C2 41 72 84 F7 C9 00 00000009 01 04 06 00 01 09 70 00 07 0B 01 00 01 23 03 01 0000000A 01 05 5D 00 00 01 00 0C 80 F5 00 08 0A 01 A8 3F 0000000B B1 B7 00 00 05 01 11 0B 00 64 00 61 00 74 00 61 0000000C 00 00 00 14 0A 01 00 68 6E B8 CF BC A0 CD 01 15 0000000D 06 01 00 20 00 00 00 00 00
Challenge Input
Give your program its own binary file, and have it print itself out!
Challenge Input Solution
This is dependent on how you write your code and what platform you are on.
Note
-
As an added bonus, attempt to print out any ASCII strings, if such data is found in your given file.