create binary file in bash












11















How can I create a binary file with consequent binary values in bash?



like:



$ hexdump testfile
0000000 0100 0302 0504 0706 0908 0b0a 0d0c 0f0e
0000010 1110 1312 1514 1716 1918 1b1a 1d1c 1f1e
0000020 2120 2322 2524 2726 2928 2b2a 2d2c 2f2e
0000030 ....


In C, I do:



fd = open("testfile", O_RDWR | O_CREAT);
for (i=0; i< CONTENT_SIZE; i++)
{
testBufOut[i] = i;
}
num_bytes_written = write(fd, testBufOut, CONTENT_SIZE);
close (fd);




this is what I wanted:



#!/bin/bash
i=0
while [ $i -lt 256 ]; do
h=$(printf "%.2Xn" $i)
echo "$h"| xxd -r -p
i=$((i-1))
done









share|improve this question




















  • 1





    Even if you probably simplified your example to make it shorter: This code doesn't check for errors AND DON'T USE write(2) because it is perfectly ok not only to fail, but also to do only partial writes. Use fwrite(3) or similar instead

    – Jo So
    Dec 15 '11 at 14:39
















11















How can I create a binary file with consequent binary values in bash?



like:



$ hexdump testfile
0000000 0100 0302 0504 0706 0908 0b0a 0d0c 0f0e
0000010 1110 1312 1514 1716 1918 1b1a 1d1c 1f1e
0000020 2120 2322 2524 2726 2928 2b2a 2d2c 2f2e
0000030 ....


In C, I do:



fd = open("testfile", O_RDWR | O_CREAT);
for (i=0; i< CONTENT_SIZE; i++)
{
testBufOut[i] = i;
}
num_bytes_written = write(fd, testBufOut, CONTENT_SIZE);
close (fd);




this is what I wanted:



#!/bin/bash
i=0
while [ $i -lt 256 ]; do
h=$(printf "%.2Xn" $i)
echo "$h"| xxd -r -p
i=$((i-1))
done









share|improve this question




















  • 1





    Even if you probably simplified your example to make it shorter: This code doesn't check for errors AND DON'T USE write(2) because it is perfectly ok not only to fail, but also to do only partial writes. Use fwrite(3) or similar instead

    – Jo So
    Dec 15 '11 at 14:39














11












11








11


0






How can I create a binary file with consequent binary values in bash?



like:



$ hexdump testfile
0000000 0100 0302 0504 0706 0908 0b0a 0d0c 0f0e
0000010 1110 1312 1514 1716 1918 1b1a 1d1c 1f1e
0000020 2120 2322 2524 2726 2928 2b2a 2d2c 2f2e
0000030 ....


In C, I do:



fd = open("testfile", O_RDWR | O_CREAT);
for (i=0; i< CONTENT_SIZE; i++)
{
testBufOut[i] = i;
}
num_bytes_written = write(fd, testBufOut, CONTENT_SIZE);
close (fd);




this is what I wanted:



#!/bin/bash
i=0
while [ $i -lt 256 ]; do
h=$(printf "%.2Xn" $i)
echo "$h"| xxd -r -p
i=$((i-1))
done









share|improve this question
















How can I create a binary file with consequent binary values in bash?



like:



$ hexdump testfile
0000000 0100 0302 0504 0706 0908 0b0a 0d0c 0f0e
0000010 1110 1312 1514 1716 1918 1b1a 1d1c 1f1e
0000020 2120 2322 2524 2726 2928 2b2a 2d2c 2f2e
0000030 ....


In C, I do:



fd = open("testfile", O_RDWR | O_CREAT);
for (i=0; i< CONTENT_SIZE; i++)
{
testBufOut[i] = i;
}
num_bytes_written = write(fd, testBufOut, CONTENT_SIZE);
close (fd);




this is what I wanted:



#!/bin/bash
i=0
while [ $i -lt 256 ]; do
h=$(printf "%.2Xn" $i)
echo "$h"| xxd -r -p
i=$((i-1))
done






bash binaryfiles hexdump






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 15 '11 at 14:29







mustafa

















asked Dec 15 '11 at 13:58









mustafamustafa

1,41152346




1,41152346








  • 1





    Even if you probably simplified your example to make it shorter: This code doesn't check for errors AND DON'T USE write(2) because it is perfectly ok not only to fail, but also to do only partial writes. Use fwrite(3) or similar instead

    – Jo So
    Dec 15 '11 at 14:39














  • 1





    Even if you probably simplified your example to make it shorter: This code doesn't check for errors AND DON'T USE write(2) because it is perfectly ok not only to fail, but also to do only partial writes. Use fwrite(3) or similar instead

    – Jo So
    Dec 15 '11 at 14:39








1




1





Even if you probably simplified your example to make it shorter: This code doesn't check for errors AND DON'T USE write(2) because it is perfectly ok not only to fail, but also to do only partial writes. Use fwrite(3) or similar instead

– Jo So
Dec 15 '11 at 14:39





Even if you probably simplified your example to make it shorter: This code doesn't check for errors AND DON'T USE write(2) because it is perfectly ok not only to fail, but also to do only partial writes. Use fwrite(3) or similar instead

– Jo So
Dec 15 '11 at 14:39












4 Answers
4






active

oldest

votes


















13














There's only 1 byte you cannot pass as argument in bash command line: 0
For any other value, you can just redirect it. It's safe.



echo -n $'x01' > binary.dat
echo -n $'x02' >> binary.dat
...


For the value 0, there's another way to output it to a file



dd if=/dev/zero of=binary.dat bs=1c count=1 


To append it to file, use



dd if=/dev/zero oflag=append conv=notrunc of=binary.dat bs=1c count=1





share|improve this answer





















  • 1





    Just dd if=/dev/zero bs=1 count=1 wothout of and oflag outputs the NUL byte to stdout. So you can do a > or >>.

    – glglgl
    Jan 12 '12 at 8:09



















8














Maybe you could take a look to xxd :




xxd : creates a hex dump of a given file or standard input. It can
also
convert a hex dump back to its original binary form.







share|improve this answer
























  • thanks, xxd was what I needed

    – mustafa
    Dec 15 '11 at 14:30






  • 2





    For those wanting to know how to use xxd to write without having to go away and look it up (like I had to): echo "0000400: 4142 4344" | xxd -r - data.bin where 0000400 is the byte offset into the file and the hex bytes 41 thru 44 are what's written (the embedded space is ignored). This example writes the string 'ABCD' at 1024 bytes into the file 'data.bin'.

    – starfry
    Jul 17 '15 at 13:16





















2














If you don't mind to not use an existing command and want to describe you data in a text file, you can use binmake that is a C++ program that you can compile and use like following:



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



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


Create your text file file.txt:



big-endian
00010203
04050607
# separated bytes not concerned by endianess
08 09 0a 0b 0c 0d 0e 0f


Generate your binary file file.bin:



$ ./binmake file.txt file.bin
$ hexdump file.bin
0000000 0100 0302 0504 0706 0908 0b0a 0d0c 0f0e
0000008


Note: you can also use it with stdin/stdout






share|improve this answer


























  • If you go this route, you probably should just use a standard tool (od for portability, or xxd for usability).

    – nabin-info
    May 27 '17 at 4:57





















0














use below command,



i=0; while [ $i -lt 256 ]; do echo -en 'x'$(printf "%0x" $i)''  >> binary.dat; i=$((i+1));  done





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8521240%2fcreate-binary-file-in-bash%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    13














    There's only 1 byte you cannot pass as argument in bash command line: 0
    For any other value, you can just redirect it. It's safe.



    echo -n $'x01' > binary.dat
    echo -n $'x02' >> binary.dat
    ...


    For the value 0, there's another way to output it to a file



    dd if=/dev/zero of=binary.dat bs=1c count=1 


    To append it to file, use



    dd if=/dev/zero oflag=append conv=notrunc of=binary.dat bs=1c count=1





    share|improve this answer





















    • 1





      Just dd if=/dev/zero bs=1 count=1 wothout of and oflag outputs the NUL byte to stdout. So you can do a > or >>.

      – glglgl
      Jan 12 '12 at 8:09
















    13














    There's only 1 byte you cannot pass as argument in bash command line: 0
    For any other value, you can just redirect it. It's safe.



    echo -n $'x01' > binary.dat
    echo -n $'x02' >> binary.dat
    ...


    For the value 0, there's another way to output it to a file



    dd if=/dev/zero of=binary.dat bs=1c count=1 


    To append it to file, use



    dd if=/dev/zero oflag=append conv=notrunc of=binary.dat bs=1c count=1





    share|improve this answer





















    • 1





      Just dd if=/dev/zero bs=1 count=1 wothout of and oflag outputs the NUL byte to stdout. So you can do a > or >>.

      – glglgl
      Jan 12 '12 at 8:09














    13












    13








    13







    There's only 1 byte you cannot pass as argument in bash command line: 0
    For any other value, you can just redirect it. It's safe.



    echo -n $'x01' > binary.dat
    echo -n $'x02' >> binary.dat
    ...


    For the value 0, there's another way to output it to a file



    dd if=/dev/zero of=binary.dat bs=1c count=1 


    To append it to file, use



    dd if=/dev/zero oflag=append conv=notrunc of=binary.dat bs=1c count=1





    share|improve this answer















    There's only 1 byte you cannot pass as argument in bash command line: 0
    For any other value, you can just redirect it. It's safe.



    echo -n $'x01' > binary.dat
    echo -n $'x02' >> binary.dat
    ...


    For the value 0, there's another way to output it to a file



    dd if=/dev/zero of=binary.dat bs=1c count=1 


    To append it to file, use



    dd if=/dev/zero oflag=append conv=notrunc of=binary.dat bs=1c count=1






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Oct 6 '14 at 9:17









    ceving

    10.4k360104




    10.4k360104










    answered Jan 12 '12 at 7:35









    zhaorufeizhaorufei

    1,3311317




    1,3311317








    • 1





      Just dd if=/dev/zero bs=1 count=1 wothout of and oflag outputs the NUL byte to stdout. So you can do a > or >>.

      – glglgl
      Jan 12 '12 at 8:09














    • 1





      Just dd if=/dev/zero bs=1 count=1 wothout of and oflag outputs the NUL byte to stdout. So you can do a > or >>.

      – glglgl
      Jan 12 '12 at 8:09








    1




    1





    Just dd if=/dev/zero bs=1 count=1 wothout of and oflag outputs the NUL byte to stdout. So you can do a > or >>.

    – glglgl
    Jan 12 '12 at 8:09





    Just dd if=/dev/zero bs=1 count=1 wothout of and oflag outputs the NUL byte to stdout. So you can do a > or >>.

    – glglgl
    Jan 12 '12 at 8:09













    8














    Maybe you could take a look to xxd :




    xxd : creates a hex dump of a given file or standard input. It can
    also
    convert a hex dump back to its original binary form.







    share|improve this answer
























    • thanks, xxd was what I needed

      – mustafa
      Dec 15 '11 at 14:30






    • 2





      For those wanting to know how to use xxd to write without having to go away and look it up (like I had to): echo "0000400: 4142 4344" | xxd -r - data.bin where 0000400 is the byte offset into the file and the hex bytes 41 thru 44 are what's written (the embedded space is ignored). This example writes the string 'ABCD' at 1024 bytes into the file 'data.bin'.

      – starfry
      Jul 17 '15 at 13:16


















    8














    Maybe you could take a look to xxd :




    xxd : creates a hex dump of a given file or standard input. It can
    also
    convert a hex dump back to its original binary form.







    share|improve this answer
























    • thanks, xxd was what I needed

      – mustafa
      Dec 15 '11 at 14:30






    • 2





      For those wanting to know how to use xxd to write without having to go away and look it up (like I had to): echo "0000400: 4142 4344" | xxd -r - data.bin where 0000400 is the byte offset into the file and the hex bytes 41 thru 44 are what's written (the embedded space is ignored). This example writes the string 'ABCD' at 1024 bytes into the file 'data.bin'.

      – starfry
      Jul 17 '15 at 13:16
















    8












    8








    8







    Maybe you could take a look to xxd :




    xxd : creates a hex dump of a given file or standard input. It can
    also
    convert a hex dump back to its original binary form.







    share|improve this answer













    Maybe you could take a look to xxd :




    xxd : creates a hex dump of a given file or standard input. It can
    also
    convert a hex dump back to its original binary form.








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 15 '11 at 14:02









    Cédric JulienCédric Julien

    55.4k12100111




    55.4k12100111













    • thanks, xxd was what I needed

      – mustafa
      Dec 15 '11 at 14:30






    • 2





      For those wanting to know how to use xxd to write without having to go away and look it up (like I had to): echo "0000400: 4142 4344" | xxd -r - data.bin where 0000400 is the byte offset into the file and the hex bytes 41 thru 44 are what's written (the embedded space is ignored). This example writes the string 'ABCD' at 1024 bytes into the file 'data.bin'.

      – starfry
      Jul 17 '15 at 13:16





















    • thanks, xxd was what I needed

      – mustafa
      Dec 15 '11 at 14:30






    • 2





      For those wanting to know how to use xxd to write without having to go away and look it up (like I had to): echo "0000400: 4142 4344" | xxd -r - data.bin where 0000400 is the byte offset into the file and the hex bytes 41 thru 44 are what's written (the embedded space is ignored). This example writes the string 'ABCD' at 1024 bytes into the file 'data.bin'.

      – starfry
      Jul 17 '15 at 13:16



















    thanks, xxd was what I needed

    – mustafa
    Dec 15 '11 at 14:30





    thanks, xxd was what I needed

    – mustafa
    Dec 15 '11 at 14:30




    2




    2





    For those wanting to know how to use xxd to write without having to go away and look it up (like I had to): echo "0000400: 4142 4344" | xxd -r - data.bin where 0000400 is the byte offset into the file and the hex bytes 41 thru 44 are what's written (the embedded space is ignored). This example writes the string 'ABCD' at 1024 bytes into the file 'data.bin'.

    – starfry
    Jul 17 '15 at 13:16







    For those wanting to know how to use xxd to write without having to go away and look it up (like I had to): echo "0000400: 4142 4344" | xxd -r - data.bin where 0000400 is the byte offset into the file and the hex bytes 41 thru 44 are what's written (the embedded space is ignored). This example writes the string 'ABCD' at 1024 bytes into the file 'data.bin'.

    – starfry
    Jul 17 '15 at 13:16













    2














    If you don't mind to not use an existing command and want to describe you data in a text file, you can use binmake that is a C++ program that you can compile and use like following:



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



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


    Create your text file file.txt:



    big-endian
    00010203
    04050607
    # separated bytes not concerned by endianess
    08 09 0a 0b 0c 0d 0e 0f


    Generate your binary file file.bin:



    $ ./binmake file.txt file.bin
    $ hexdump file.bin
    0000000 0100 0302 0504 0706 0908 0b0a 0d0c 0f0e
    0000008


    Note: you can also use it with stdin/stdout






    share|improve this answer


























    • If you go this route, you probably should just use a standard tool (od for portability, or xxd for usability).

      – nabin-info
      May 27 '17 at 4:57


















    2














    If you don't mind to not use an existing command and want to describe you data in a text file, you can use binmake that is a C++ program that you can compile and use like following:



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



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


    Create your text file file.txt:



    big-endian
    00010203
    04050607
    # separated bytes not concerned by endianess
    08 09 0a 0b 0c 0d 0e 0f


    Generate your binary file file.bin:



    $ ./binmake file.txt file.bin
    $ hexdump file.bin
    0000000 0100 0302 0504 0706 0908 0b0a 0d0c 0f0e
    0000008


    Note: you can also use it with stdin/stdout






    share|improve this answer


























    • If you go this route, you probably should just use a standard tool (od for portability, or xxd for usability).

      – nabin-info
      May 27 '17 at 4:57
















    2












    2








    2







    If you don't mind to not use an existing command and want to describe you data in a text file, you can use binmake that is a C++ program that you can compile and use like following:



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



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


    Create your text file file.txt:



    big-endian
    00010203
    04050607
    # separated bytes not concerned by endianess
    08 09 0a 0b 0c 0d 0e 0f


    Generate your binary file file.bin:



    $ ./binmake file.txt file.bin
    $ hexdump file.bin
    0000000 0100 0302 0504 0706 0908 0b0a 0d0c 0f0e
    0000008


    Note: you can also use it with stdin/stdout






    share|improve this answer















    If you don't mind to not use an existing command and want to describe you data in a text file, you can use binmake that is a C++ program that you can compile and use like following:



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



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


    Create your text file file.txt:



    big-endian
    00010203
    04050607
    # separated bytes not concerned by endianess
    08 09 0a 0b 0c 0d 0e 0f


    Generate your binary file file.bin:



    $ ./binmake file.txt file.bin
    $ hexdump file.bin
    0000000 0100 0302 0504 0706 0908 0b0a 0d0c 0f0e
    0000008


    Note: you can also use it with stdin/stdout







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 12 '17 at 12:36

























    answered Jun 17 '16 at 13:35









    daouzlidaouzli

    8,73511215




    8,73511215













    • If you go this route, you probably should just use a standard tool (od for portability, or xxd for usability).

      – nabin-info
      May 27 '17 at 4:57





















    • If you go this route, you probably should just use a standard tool (od for portability, or xxd for usability).

      – nabin-info
      May 27 '17 at 4:57



















    If you go this route, you probably should just use a standard tool (od for portability, or xxd for usability).

    – nabin-info
    May 27 '17 at 4:57







    If you go this route, you probably should just use a standard tool (od for portability, or xxd for usability).

    – nabin-info
    May 27 '17 at 4:57













    0














    use below command,



    i=0; while [ $i -lt 256 ]; do echo -en 'x'$(printf "%0x" $i)''  >> binary.dat; i=$((i+1));  done





    share|improve this answer






























      0














      use below command,



      i=0; while [ $i -lt 256 ]; do echo -en 'x'$(printf "%0x" $i)''  >> binary.dat; i=$((i+1));  done





      share|improve this answer




























        0












        0








        0







        use below command,



        i=0; while [ $i -lt 256 ]; do echo -en 'x'$(printf "%0x" $i)''  >> binary.dat; i=$((i+1));  done





        share|improve this answer















        use below command,



        i=0; while [ $i -lt 256 ]; do echo -en 'x'$(printf "%0x" $i)''  >> binary.dat; i=$((i+1));  done






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 6:53

























        answered Nov 15 '18 at 21:42









        Prashant AdlingePrashant Adlinge

        11




        11






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8521240%2fcreate-binary-file-in-bash%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python