How to write a binary file using Bash?

后端 未结 6 1842
迷失自我
迷失自我 2021-02-05 05:46

My problem is that I need to create a file with this exact bytes: 48, 00, 49, 00.

I cannot use C, perl, other scripting language (the target is an embedded

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-05 06:36

    I needed to write binary files from hex using busybox within an old Android shell. This printf with a redirect worked in my use case.

    Write the binary data in hex:

    # busybox printf '\x74\x65\x73\x74' > /sdcard/test.txt
    

    Display the result in hex:

    # busybox hexdump -C /sdcard/test.txt
    00000000  74 65 73 74                                       |test|
    00000004
    

    Display the result in ascii:

    # cat /sdcard/test.txt
    test
    

提交回复
热议问题