Is there a oneliner that converts a binary file from little endian to big endian?Why is od calculating decimal values wrong?What options does wget --report-speed take?How do I trim bytes from the beginning and end of a file?

How badly should I try to prevent a user from XSSing themselves?

Examples of smooth manifolds admitting inbetween one and a continuum of complex structures

Forgetting the musical notes while performing in concert

Intersection Puzzle

Why is this clock signal connected to a capacitor to gnd?

Is there an expression that means doing something right before you will need it rather than doing it in case you might need it?

How did the Super Star Destroyer Executor get destroyed exactly?

How would I stat a creature to be immune to everything but the Magic Missile spell? (just for fun)

A category-like structure without composition?

Dreadful Dastardly Diseases, or Always Atrocious Ailments

Venezuelan girlfriend wants to travel the USA to be with me. What is the process?

What killed these X2 caps?

Is there a hemisphere-neutral way of specifying a season?

Detention in 1997

How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?

Arrow those variables!

Assassin's bullet with mercury

Can a virus destroy the BIOS of a modern computer?

How can I determine if the org that I'm currently connected to is a scratch org?

Could the museum Saturn V's be refitted for one more flight?

How seriously should I take size and weight limits of hand luggage?

Why doesn't using multiple commands with a || or && conditional work?

Mathematica command that allows it to read my intentions

Little known, relatively unlikely, but scientifically plausible, apocalyptic (or near apocalyptic) events



Is there a oneliner that converts a binary file from little endian to big endian?


Why is od calculating decimal values wrong?What options does wget --report-speed take?How do I trim bytes from the beginning and end of a file?













12















and vice versa.



I am running a RedHat if relevant.










share|improve this question

















  • 6





    What type of binary file?

    – Stéphane Chazelas
    Oct 29 '15 at 16:47






  • 1





    In any case, for any file format, I'm sure you could write a one-liner to convert its endianness using Perl and pack / unpack. For some formats, it'll just be a longer line than for others. ;-)

    – Ilmari Karonen
    Oct 29 '15 at 21:13
















12















and vice versa.



I am running a RedHat if relevant.










share|improve this question

















  • 6





    What type of binary file?

    – Stéphane Chazelas
    Oct 29 '15 at 16:47






  • 1





    In any case, for any file format, I'm sure you could write a one-liner to convert its endianness using Perl and pack / unpack. For some formats, it'll just be a longer line than for others. ;-)

    – Ilmari Karonen
    Oct 29 '15 at 21:13














12












12








12


3






and vice versa.



I am running a RedHat if relevant.










share|improve this question














and vice versa.



I am running a RedHat if relevant.







byte






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 29 '15 at 16:22









WillWill

2801210




2801210







  • 6





    What type of binary file?

    – Stéphane Chazelas
    Oct 29 '15 at 16:47






  • 1





    In any case, for any file format, I'm sure you could write a one-liner to convert its endianness using Perl and pack / unpack. For some formats, it'll just be a longer line than for others. ;-)

    – Ilmari Karonen
    Oct 29 '15 at 21:13













  • 6





    What type of binary file?

    – Stéphane Chazelas
    Oct 29 '15 at 16:47






  • 1





    In any case, for any file format, I'm sure you could write a one-liner to convert its endianness using Perl and pack / unpack. For some formats, it'll just be a longer line than for others. ;-)

    – Ilmari Karonen
    Oct 29 '15 at 21:13








6




6





What type of binary file?

– Stéphane Chazelas
Oct 29 '15 at 16:47





What type of binary file?

– Stéphane Chazelas
Oct 29 '15 at 16:47




1




1





In any case, for any file format, I'm sure you could write a one-liner to convert its endianness using Perl and pack / unpack. For some formats, it'll just be a longer line than for others. ;-)

– Ilmari Karonen
Oct 29 '15 at 21:13






In any case, for any file format, I'm sure you could write a one-liner to convert its endianness using Perl and pack / unpack. For some formats, it'll just be a longer line than for others. ;-)

– Ilmari Karonen
Oct 29 '15 at 21:13











4 Answers
4






active

oldest

votes


















33














You cannot do this because for such a conversion, you need to know the meaning of the binary content.



If e.g. there is a string inside a binary file it must not be converted and a 4 byte integer may need different treatment than a two byte integer.



In other words, for a byte order conversion, you need a data type description.






share|improve this answer


















  • 7





    I was just to say that. it is not as simple as just swapping every pair of bytes. for example a 4 byte integer 01 02 03 04 in bigendian could look like this in little endian 04 02 03 01 NOT 02 01 04 03 cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html

    – Rob
    Oct 29 '15 at 16:36







  • 1





    Agreed, endianness makes no sense on a raw, typeless byte stream... this should be the accepted answer if OP does not edit his question

    – Thomas
    Oct 30 '15 at 11:02











  • [citation needed] - AFAIK, unless an application explicitly preserves some byte order and not others, then it is usual for bytes or words to be consistently ordered throughout.

    – Dennis Williamson
    Oct 30 '15 at 15:52






  • 1





    @DennisWilliamson: A UTF-8 or ASCII string does not have endianness. UTF-8 specifies byte order while ASCII is 7 bit and doesn't have multi-byte tokens. A UTF-16 string does have endianness and may or may not have a BOM to mark that endianness. A Unix binary which is intended to interact with Windows or Java might have both types of string, which means you have to distinguish them when converting.

    – Kevin
    Oct 30 '15 at 18:42












  • I came here looking for a program where you give it the data description and the data and it converts it. Thus far I have not found such a program but that does not mean it cannot be done.

    – Levi Morrison
    Aug 10 '17 at 16:37



















32














You can byteswap with dd. Is that sufficent? If not, please update your question to give an example of an input file and the expected outfile.



echo hello >infile
dd conv=swab <infile >outfile

hex infile
0000 68 65 6c 6c 6f 0a hello.
hex outfile
0000 65 68 6c 6c 0a 6f ehll.o





share|improve this answer
































    2














    If you don't care about file contents and just want to swap bytes, then try endconv. It is just a wrapper around standard byte conversion functions, so it supports conversion by 2, 4 and 8 byte long integers. It's not one liner though because it is separate program.






    share|improve this answer






























      0














      In order to change file endianess, assuming word (32-bit) size, this 1 liner should work for you:



      hexdump -v -e '1/4 "%08x"' -e '"n"' input_file | xxd -r -p > output_file





      share|improve this answer























        Your Answer








        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "106"
        ;
        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: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        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%2funix.stackexchange.com%2fquestions%2f239543%2fis-there-a-oneliner-that-converts-a-binary-file-from-little-endian-to-big-endian%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









        33














        You cannot do this because for such a conversion, you need to know the meaning of the binary content.



        If e.g. there is a string inside a binary file it must not be converted and a 4 byte integer may need different treatment than a two byte integer.



        In other words, for a byte order conversion, you need a data type description.






        share|improve this answer


















        • 7





          I was just to say that. it is not as simple as just swapping every pair of bytes. for example a 4 byte integer 01 02 03 04 in bigendian could look like this in little endian 04 02 03 01 NOT 02 01 04 03 cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html

          – Rob
          Oct 29 '15 at 16:36







        • 1





          Agreed, endianness makes no sense on a raw, typeless byte stream... this should be the accepted answer if OP does not edit his question

          – Thomas
          Oct 30 '15 at 11:02











        • [citation needed] - AFAIK, unless an application explicitly preserves some byte order and not others, then it is usual for bytes or words to be consistently ordered throughout.

          – Dennis Williamson
          Oct 30 '15 at 15:52






        • 1





          @DennisWilliamson: A UTF-8 or ASCII string does not have endianness. UTF-8 specifies byte order while ASCII is 7 bit and doesn't have multi-byte tokens. A UTF-16 string does have endianness and may or may not have a BOM to mark that endianness. A Unix binary which is intended to interact with Windows or Java might have both types of string, which means you have to distinguish them when converting.

          – Kevin
          Oct 30 '15 at 18:42












        • I came here looking for a program where you give it the data description and the data and it converts it. Thus far I have not found such a program but that does not mean it cannot be done.

          – Levi Morrison
          Aug 10 '17 at 16:37
















        33














        You cannot do this because for such a conversion, you need to know the meaning of the binary content.



        If e.g. there is a string inside a binary file it must not be converted and a 4 byte integer may need different treatment than a two byte integer.



        In other words, for a byte order conversion, you need a data type description.






        share|improve this answer


















        • 7





          I was just to say that. it is not as simple as just swapping every pair of bytes. for example a 4 byte integer 01 02 03 04 in bigendian could look like this in little endian 04 02 03 01 NOT 02 01 04 03 cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html

          – Rob
          Oct 29 '15 at 16:36







        • 1





          Agreed, endianness makes no sense on a raw, typeless byte stream... this should be the accepted answer if OP does not edit his question

          – Thomas
          Oct 30 '15 at 11:02











        • [citation needed] - AFAIK, unless an application explicitly preserves some byte order and not others, then it is usual for bytes or words to be consistently ordered throughout.

          – Dennis Williamson
          Oct 30 '15 at 15:52






        • 1





          @DennisWilliamson: A UTF-8 or ASCII string does not have endianness. UTF-8 specifies byte order while ASCII is 7 bit and doesn't have multi-byte tokens. A UTF-16 string does have endianness and may or may not have a BOM to mark that endianness. A Unix binary which is intended to interact with Windows or Java might have both types of string, which means you have to distinguish them when converting.

          – Kevin
          Oct 30 '15 at 18:42












        • I came here looking for a program where you give it the data description and the data and it converts it. Thus far I have not found such a program but that does not mean it cannot be done.

          – Levi Morrison
          Aug 10 '17 at 16:37














        33












        33








        33







        You cannot do this because for such a conversion, you need to know the meaning of the binary content.



        If e.g. there is a string inside a binary file it must not be converted and a 4 byte integer may need different treatment than a two byte integer.



        In other words, for a byte order conversion, you need a data type description.






        share|improve this answer













        You cannot do this because for such a conversion, you need to know the meaning of the binary content.



        If e.g. there is a string inside a binary file it must not be converted and a 4 byte integer may need different treatment than a two byte integer.



        In other words, for a byte order conversion, you need a data type description.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 29 '15 at 16:33









        schilyschily

        10.9k31744




        10.9k31744







        • 7





          I was just to say that. it is not as simple as just swapping every pair of bytes. for example a 4 byte integer 01 02 03 04 in bigendian could look like this in little endian 04 02 03 01 NOT 02 01 04 03 cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html

          – Rob
          Oct 29 '15 at 16:36







        • 1





          Agreed, endianness makes no sense on a raw, typeless byte stream... this should be the accepted answer if OP does not edit his question

          – Thomas
          Oct 30 '15 at 11:02











        • [citation needed] - AFAIK, unless an application explicitly preserves some byte order and not others, then it is usual for bytes or words to be consistently ordered throughout.

          – Dennis Williamson
          Oct 30 '15 at 15:52






        • 1





          @DennisWilliamson: A UTF-8 or ASCII string does not have endianness. UTF-8 specifies byte order while ASCII is 7 bit and doesn't have multi-byte tokens. A UTF-16 string does have endianness and may or may not have a BOM to mark that endianness. A Unix binary which is intended to interact with Windows or Java might have both types of string, which means you have to distinguish them when converting.

          – Kevin
          Oct 30 '15 at 18:42












        • I came here looking for a program where you give it the data description and the data and it converts it. Thus far I have not found such a program but that does not mean it cannot be done.

          – Levi Morrison
          Aug 10 '17 at 16:37













        • 7





          I was just to say that. it is not as simple as just swapping every pair of bytes. for example a 4 byte integer 01 02 03 04 in bigendian could look like this in little endian 04 02 03 01 NOT 02 01 04 03 cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html

          – Rob
          Oct 29 '15 at 16:36







        • 1





          Agreed, endianness makes no sense on a raw, typeless byte stream... this should be the accepted answer if OP does not edit his question

          – Thomas
          Oct 30 '15 at 11:02











        • [citation needed] - AFAIK, unless an application explicitly preserves some byte order and not others, then it is usual for bytes or words to be consistently ordered throughout.

          – Dennis Williamson
          Oct 30 '15 at 15:52






        • 1





          @DennisWilliamson: A UTF-8 or ASCII string does not have endianness. UTF-8 specifies byte order while ASCII is 7 bit and doesn't have multi-byte tokens. A UTF-16 string does have endianness and may or may not have a BOM to mark that endianness. A Unix binary which is intended to interact with Windows or Java might have both types of string, which means you have to distinguish them when converting.

          – Kevin
          Oct 30 '15 at 18:42












        • I came here looking for a program where you give it the data description and the data and it converts it. Thus far I have not found such a program but that does not mean it cannot be done.

          – Levi Morrison
          Aug 10 '17 at 16:37








        7




        7





        I was just to say that. it is not as simple as just swapping every pair of bytes. for example a 4 byte integer 01 02 03 04 in bigendian could look like this in little endian 04 02 03 01 NOT 02 01 04 03 cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html

        – Rob
        Oct 29 '15 at 16:36






        I was just to say that. it is not as simple as just swapping every pair of bytes. for example a 4 byte integer 01 02 03 04 in bigendian could look like this in little endian 04 02 03 01 NOT 02 01 04 03 cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html

        – Rob
        Oct 29 '15 at 16:36





        1




        1





        Agreed, endianness makes no sense on a raw, typeless byte stream... this should be the accepted answer if OP does not edit his question

        – Thomas
        Oct 30 '15 at 11:02





        Agreed, endianness makes no sense on a raw, typeless byte stream... this should be the accepted answer if OP does not edit his question

        – Thomas
        Oct 30 '15 at 11:02













        [citation needed] - AFAIK, unless an application explicitly preserves some byte order and not others, then it is usual for bytes or words to be consistently ordered throughout.

        – Dennis Williamson
        Oct 30 '15 at 15:52





        [citation needed] - AFAIK, unless an application explicitly preserves some byte order and not others, then it is usual for bytes or words to be consistently ordered throughout.

        – Dennis Williamson
        Oct 30 '15 at 15:52




        1




        1





        @DennisWilliamson: A UTF-8 or ASCII string does not have endianness. UTF-8 specifies byte order while ASCII is 7 bit and doesn't have multi-byte tokens. A UTF-16 string does have endianness and may or may not have a BOM to mark that endianness. A Unix binary which is intended to interact with Windows or Java might have both types of string, which means you have to distinguish them when converting.

        – Kevin
        Oct 30 '15 at 18:42






        @DennisWilliamson: A UTF-8 or ASCII string does not have endianness. UTF-8 specifies byte order while ASCII is 7 bit and doesn't have multi-byte tokens. A UTF-16 string does have endianness and may or may not have a BOM to mark that endianness. A Unix binary which is intended to interact with Windows or Java might have both types of string, which means you have to distinguish them when converting.

        – Kevin
        Oct 30 '15 at 18:42














        I came here looking for a program where you give it the data description and the data and it converts it. Thus far I have not found such a program but that does not mean it cannot be done.

        – Levi Morrison
        Aug 10 '17 at 16:37






        I came here looking for a program where you give it the data description and the data and it converts it. Thus far I have not found such a program but that does not mean it cannot be done.

        – Levi Morrison
        Aug 10 '17 at 16:37














        32














        You can byteswap with dd. Is that sufficent? If not, please update your question to give an example of an input file and the expected outfile.



        echo hello >infile
        dd conv=swab <infile >outfile

        hex infile
        0000 68 65 6c 6c 6f 0a hello.
        hex outfile
        0000 65 68 6c 6c 0a 6f ehll.o





        share|improve this answer





























          32














          You can byteswap with dd. Is that sufficent? If not, please update your question to give an example of an input file and the expected outfile.



          echo hello >infile
          dd conv=swab <infile >outfile

          hex infile
          0000 68 65 6c 6c 6f 0a hello.
          hex outfile
          0000 65 68 6c 6c 0a 6f ehll.o





          share|improve this answer



























            32












            32








            32







            You can byteswap with dd. Is that sufficent? If not, please update your question to give an example of an input file and the expected outfile.



            echo hello >infile
            dd conv=swab <infile >outfile

            hex infile
            0000 68 65 6c 6c 6f 0a hello.
            hex outfile
            0000 65 68 6c 6c 0a 6f ehll.o





            share|improve this answer















            You can byteswap with dd. Is that sufficent? If not, please update your question to give an example of an input file and the expected outfile.



            echo hello >infile
            dd conv=swab <infile >outfile

            hex infile
            0000 68 65 6c 6c 6f 0a hello.
            hex outfile
            0000 65 68 6c 6c 0a 6f ehll.o






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 29 '15 at 16:57









            mikeserv

            46k669162




            46k669162










            answered Oct 29 '15 at 16:28









            roaimaroaima

            46k758124




            46k758124





















                2














                If you don't care about file contents and just want to swap bytes, then try endconv. It is just a wrapper around standard byte conversion functions, so it supports conversion by 2, 4 and 8 byte long integers. It's not one liner though because it is separate program.






                share|improve this answer



























                  2














                  If you don't care about file contents and just want to swap bytes, then try endconv. It is just a wrapper around standard byte conversion functions, so it supports conversion by 2, 4 and 8 byte long integers. It's not one liner though because it is separate program.






                  share|improve this answer

























                    2












                    2








                    2







                    If you don't care about file contents and just want to swap bytes, then try endconv. It is just a wrapper around standard byte conversion functions, so it supports conversion by 2, 4 and 8 byte long integers. It's not one liner though because it is separate program.






                    share|improve this answer













                    If you don't care about file contents and just want to swap bytes, then try endconv. It is just a wrapper around standard byte conversion functions, so it supports conversion by 2, 4 and 8 byte long integers. It's not one liner though because it is separate program.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Oct 30 '15 at 4:00







                    user140866




























                        0














                        In order to change file endianess, assuming word (32-bit) size, this 1 liner should work for you:



                        hexdump -v -e '1/4 "%08x"' -e '"n"' input_file | xxd -r -p > output_file





                        share|improve this answer



























                          0














                          In order to change file endianess, assuming word (32-bit) size, this 1 liner should work for you:



                          hexdump -v -e '1/4 "%08x"' -e '"n"' input_file | xxd -r -p > output_file





                          share|improve this answer

























                            0












                            0








                            0







                            In order to change file endianess, assuming word (32-bit) size, this 1 liner should work for you:



                            hexdump -v -e '1/4 "%08x"' -e '"n"' input_file | xxd -r -p > output_file





                            share|improve this answer













                            In order to change file endianess, assuming word (32-bit) size, this 1 liner should work for you:



                            hexdump -v -e '1/4 "%08x"' -e '"n"' input_file | xxd -r -p > output_file






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 27 '18 at 18:27









                            hesham_EEhesham_EE

                            1262




                            1262



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                • 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%2funix.stackexchange.com%2fquestions%2f239543%2fis-there-a-oneliner-that-converts-a-binary-file-from-little-endian-to-big-endian%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

                                getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

                                Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

                                Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.