Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Question:

  • Is there a known tool to convert a file consisting of 2 byte Hex into ascii?

Note: - Maintain file offset listing in bytes

Example:

File contents:

00000000  0054 0065 0073 0074 0020 0054 0065 0073
00000008  0074 0020 0054 0065 0073 0074 0020 0054
00000016  0065 0073 0074 0020 0054 0065 0073 0074
00000024  0020 0054 0065 0073 0074 0020 0054 0065
00000032  0073 0074 0020 0054 0065 0073 0074 0020
00000040  0054 0065 0073 0074 000a 0054 0065 0073
00000048  0074 0020 0054 0065 0073 0074 0020 0054
00000056  0065 0073 0074 0020 0054 0065 0073 0074
00000064  0020 0054 0065 0073 0074 0020 0054 0065

Expected output

00000016  0065 0073 0074 0020 0054 0065 0073 0074  |est Test Test Te|
00000032  0073 0074 0020 0054 0065 0073 0074 0020  |st Test Test.Tes|
00000048  0074 0020 0054 0065 0073 0074 0020 0054  |t Test Test Test|
00000064  0020 0054 0065 0073 0074 0020 0054 0065  | Test Test Test |
share|improve this question

3 Answers 3

up vote 2 down vote accepted

Your input file looks like it was produced using something like this:

hexdump -e '"%08_ad  "  8/1 "%04x "' -e '"" 0/0 "" "\n"' original_file

Unfortunately, xxd -r can't deal with decimal offsets.

Here is a short Gnu AWK program to give you the output you're looking for:

gawk '{printf "%s  |", $0; for (f=2; f<=9; f++) { c = strtonum("0x" $f); if (c >= 32 && c <= 126) printf "%c",c; else printf "."}; printf "|\n"}' input_file

If you're using an AWK other than gawk, you can use the strtonum() function here.

Here's another way to do the same thing as the gawk script above:

cut -c 11- input_file | sed 's/\<00//g' | xxd -r -p | hexdump -e '"%08_ad  "  8/1 "%04x " ""' -e '"  |" 8/1 "%_p" "|\n"'

If, instead, you want to convert your input file to text:

cut -c 11- input_file | xxd -r -p
share|improve this answer
    
Thanks so much :-) –  Aaron Feb 10 '11 at 14:54
    
@Aaron: I added some additional information. –  Dennis Williamson Feb 10 '11 at 15:29
iconv -f utf-16be -t ascii input.txt
share|improve this answer

To remove 0x00, on unix you can use:

tr -d '\0'

To display you can use object dump (od) and filter the output.

od -w$1 -v -t x1a $2

This gives you the raw output which you can then filter with awk (or other script).

od -w$1 -v -t x1a $2 | awk '$0~/^[0-9A-Fa-f]/ {for (i=2; i<=NF; i++) {printf "%2s ", $i; if (i%2) printf " "}; printf "  "; next} {for (i=1; i<=NF; i++) {printf "%4s ", $i; if (!(i%2)) printf "  "}; printf "\n"}'

Replace $2 with the amount of bytes you want displayed. Gives you the first step, you then need to filter every second byte (0x00)

I hope this helps.

The example below is on plain ascii, but doing so on a 2-byte file would be very much the same.

00000000  23 21 2f 62 69 6e 2f 73  68 0d 0a 0d 0a 23 20 6f  |#!/bin/sh....# o|
00000010  64 20 2d 77 24 31 20 2d  76 20 2d 74 20 78 31 61  |d -w$1 -v -t x1a|
00000020  20 24 32 20 7c 20 61 77  6b 20 27 24 30 7e 2f 5e  | $2 | awk '$0~/^|
00000030  5b 30 2d 39 41 2d 46 61  2d 66 5d 2f 20 7b 70 72  |[0-9A-Fa-f]/ {pr|
00000040  69 6e 74 66 20 22 25 73  22 2c 20 24 30 20 3b 20  |intf "%s", $0 ; |
00000050  6e 65 78 74 7d 20 7b 70  72 69 6e 74 7d 27 20 7c  |next} {print}' ||
00000060  20 73 65 64 20 27 73 2f  5e 5b 30 2d 39 61 2d 66  | sed 's/^[0-9a-f|
00000070  41 2d 46 5d 2a 5b 20 09  5d 2a 2f 2f 27 20 7c 20  |A-F]*[ .]*//' | |
00000080  73 65 64 20 27 73 2f 5b  20 09 5d 5b 20 09 5d 2a  |sed 's/[ .][ .]*|
00000090  2f 09 2f 67 27 0d 0a 6f  64 20 2d 77 24 31 20 2d  |/./g'..od -w$1 -|
000000a0  76 20 2d 74 20 78 31 61  20 24 32 20 7c 20 61 77  |v -t x1a $2 | aw|
000000b0  6b 20 27 24 30 7e 2f 5e  5b 30 2d 39 41 2d 46 61  |k '$0~/^[0-9A-Fa|
000000c0  2d 66 5d 2f 20 7b 66 6f  72 20 28 69 3d 32 3b 20  |-f]/ {for (i=2; |
000000d0  69 3c 3d 4e 46 3b 20 69  2b 2b 29 20 7b 70 72 69  |i<=NF; i++) {pri|
000000e0  6e 74 66 20 22 25 32 73  20 22 2c 20 24 69 3b 20  |ntf "%2s ", $i; |
000000f0  69 66 20 28 69 25 32 29  20 70 72 69 6e 74 66 20  |if (i%2) printf |
00000100  22 20 22 7d 3b 20 70 72  69 6e 74 66 20 22 20 20  |" "}; printf "  |
00000110  22 3b 20 6e 65 78 74 7d  20 7b 66 6f 72 20 28 69  |"; next} {for (i|
00000120  3d 31 3b 20 69 3c 3d 4e  46 3b 20 69 2b 2b 29 20  |=1; i<=NF; i++) |
00000130  7b 70 72 69 6e 74 66 20  22 25 34 73 20 22 2c 20  |{printf "%4s ", |
00000140  24 69 3b 20 69 66 20 28  21 28 69 25 32 29 29 20  |$i; if (!(i%2)) |
00000150  70 72 69 6e 74 66 20 22  20 20 22 7d 3b 20 70 72  |printf "  "}; pr|
00000160  69 6e 74 66 20 22 5c 6e  22 7d 27 0d 0a           |intf "\n"}'..|
0000016d
share|improve this answer
    
can you provide an example? –  Aaron Feb 10 '11 at 13:41
    
Looks like ignacio got a much simpler solution though... –  asoundmove Feb 10 '11 at 13:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.