Create Binary files in UNIX

后端 未结 5 1925
-上瘾入骨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:28

    There's a tool binmake allowing to describe in text format some binary data and generate a binary file (or output to stdout). It allows to change the endianess and number formats and accepts comments.

    First get and compile binmake (the binary program will be in bin/):

    $ git clone https://github.com/dadadel/binmake
    $ cd binmake
    $ make
    

    Create your text file file.txt:

    # an exemple of file description of binary data to generate
    # set endianess to big-endian
    big-endian
    
    # default number is hexadecimal
    00112233
    
    # man can explicit a number type: %b means binary number
    %b0100110111100000
    
    # change endianess to little-endian
    little-endian
    
    # if no explicit, use default
    44556677
    
    # bytes are not concerned by endianess
    88 99 aa bb
    
    # change default to decimal
    decimal
    
    # following number is now decimal
    0123
    
    # strings are delimited by " or '
    "this is some raw string"
    
    # explicit hexa number starts with %x
    %xff
    

    Generate your binary file file.bin:

    $ ./binmake file.txt file.bin
    $ hexdump file.bin -C
    00000000  00 11 22 33 4d e0 77 66  55 44 88 99 aa bb 7b 74  |.."3M.wfUD....{t|
    00000010  68 69 73 20 69 73 20 73  6f 6d 65 20 72 61 77 20  |his is some raw |
    00000020  73 74 72 69 6e 67 ff                              |string.|
    00000027
    

    You can also pipe it using stdin and stdout:

    $ echo '32 decimal 32 %x61 61' | ./binmake | hexdump -C
    00000000  32 20 61 3d                                       |2 a=|
    00000004
    

提交回复
热议问题