Create Binary files in UNIX

后端 未结 5 1926
-上瘾入骨i
-上瘾入骨i 2021-02-06 00:39

This question was out there for a while and I thought I should offer some bonus points if I can get it to work.

What did I do…

Recently at work, I wrote a par

5条回答
  •  萌比男神i
    2021-02-06 01:31

    Using cut and awk, you can do it fairly simply using a gawk (GNU Awk) extension function, strtonum():

    cut -c11-60 inputfile |
    awk '{ for (i = 1; i <= NF; i++)
           {
               c = strtonum("0x" $i)
               printf("%c", c);
           }
         }' > outputfile
    

    Or, if you are using a non-GNU version of 'new awk', then you can use:

    cut -c11-60 inputfile |
    awk '{  for (i = 1; i <= NF; i++)
            {
                s = toupper($i)
                c0 = index("0123456789ABCDEF", substr(s, 1, 1)) - 1
                c1 = index("0123456789ABCDEF", substr(s, 2, 1)) - 1
                printf("%c", c0*16 + c1);
            }
         }' > outputfile
    

    If you want to use other tools (Perl and Python sprint to mind; Ruby would be another possibility), you can do it easily enough.

    odx is a program similar to the hexdump program. The script above was modified to read 'hexdump.out' as the input file, and the output piped into odx instead of a file, and gives the following output:

    $ cat hexdump.out
    00000000  3d 3d 01 fc 73 47 54 50  02 f1 d6 55 3c 9f 49 9c  |==..sGTP...U<.I.|
    00000010  00 01 01 00 01 80 00 dc  ce 81 2f 00 00 00 00 00  |........../.....|
    00000020  00 00 00 00 00 00 00 00  ca 04 d2 33 00 00 00 00  |...........3....|
    00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 10  |................|
    00000040  01 01 0f 00 00 07 04 ea  00 00 ff ff 00 00 14 b7  |................|
    00000050  00 00 ff ff 00 00 83 ec  00 00 83 62 54 14 59 00  |...........bT.Y.|
    00000060  60 38 34 f5 01 01 0b 58  62 70 11 60 f6 ff ff ff  |`84....Xbp.`....|
    00000070  ff ff ff 02 00 7c 00 d0  01 4c 00 b0 40 40 28 02  |.....|...L..@@(.|
    $ sh -x revdump.sh | odx
    + cut -c11-60 hexdump.out
    + awk '{  for (i = 1; i <= NF; i++)
            {
                #c = strtonum("0x" $i)
                #printf("%c", c);
                s = toupper($i)
                c0 = index("0123456789ABCDEF", substr(s, 1, 1)) - 1
                c1 = index("0123456789ABCDEF", substr(s, 2, 1)) - 1
                printf("%c", c0*16 + c1);
            }
         }'
    0x0000: 3D 3D 01 FC 73 47 54 50 02 F1 D6 55 3C 9F 49 9C   ==..sGTP...U<.I.
    0x0010: 00 01 01 00 01 80 00 DC CE 81 2F 00 00 00 00 00   ........../.....
    0x0020: 00 00 00 00 00 00 00 00 CA 04 D2 33 00 00 00 00   ...........3....
    0x0030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10   ................
    0x0040: 01 01 0F 00 00 07 04 EA 00 00 FF FF 00 00 14 B7   ................
    0x0050: 00 00 FF FF 00 00 83 EC 00 00 83 62 54 14 59 00   ...........bT.Y.
    0x0060: 60 38 34 F5 01 01 0B 58 62 70 11 60 F6 FF FF FF   `84....Xbp.`....
    0x0070: FF FF FF 02 00 7C 00 D0 01 4C 00 B0 40 40 28 02   .....|...L..@@(.
    0x0080:
    $ 
    

    Or, using hexdump -C in place of odx:

    $ sh -x revdump.sh | hexdump -C
    + cut -c11-60 hexdump.out
    + awk '{  for (i = 1; i <= NF; i++)
            {
                #c = strtonum("0x" $i)
                #printf("%c", c);
                s = toupper($i)
                c0 = index("0123456789ABCDEF", substr(s, 1, 1)) - 1
                c1 = index("0123456789ABCDEF", substr(s, 2, 1)) - 1
                printf("%c", c0*16 + c1);
            }
         }'
    00000000  3d 3d 01 fc 73 47 54 50  02 f1 d6 55 3c 9f 49 9c  |==..sGTP...U<.I.|
    00000010  00 01 01 00 01 80 00 dc  ce 81 2f 00 00 00 00 00  |........../.....|
    00000020  00 00 00 00 00 00 00 00  ca 04 d2 33 00 00 00 00  |...........3....|
    00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 10  |................|
    00000040  01 01 0f 00 00 07 04 ea  00 00 ff ff 00 00 14 b7  |................|
    00000050  00 00 ff ff 00 00 83 ec  00 00 83 62 54 14 59 00  |...........bT.Y.|
    00000060  60 38 34 f5 01 01 0b 58  62 70 11 60 f6 ff ff ff  |`84....Xbp.`....|
    00000070  ff ff ff 02 00 7c 00 d0  01 4c 00 b0 40 40 28 02  |.....|...L..@@(.|
    00000080
    $
    

提交回复
热议问题