Why flipping is done on data from Big endian before storing in LE? 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 Results Why I closed the “Why is Kali so hard” questionSize of data that can be written to / read from socketsPhysical layer header is not present in the data read from raw sockets?How to send data from serial port over wifi?Emulate console by sending data on one UDP port, and receive text from anotherHow to pass received data from netcat to another script as argument?Why doesn't file redirection to and from a named pipe work, but piping to cat does?When does the kernel cut UDP datagram into MTU chunks?

Is there a service that would inform me whenever a new direct route is scheduled from a given airport?

How to market an anarchic city as a tourism spot to people living in civilized areas?

Writing Thesis: Copying from published papers

Replacing HDD with SSD; what about non-APFS/APFS?

How can players take actions together that are impossible otherwise?

Passing functions in C++

Unexpected result with right shift after bitwise negation

How do I automatically answer y in bash script?

How to rotate it perfectly?

How should I respond to a player wanting to catch a sword between their hands?

When is phishing education going too far?

Antler Helmet: Can it work?

Limit for e and 1/e

How do you clear the ApexPages.getMessages() collection in a test?

How is simplicity better than precision and clarity in prose?

What's the difference between (size_t)-1 and ~0?

Can I throw a longsword at someone?

Why is "Captain Marvel" translated as male in Portugal?

Who can trigger ship-wide alerts in Star Trek?

New Order #5: where Fibonacci and Beatty meet at Wythoff

Can a monk deflect thrown melee weapons?

What is the largest species of polychaete?

What to do with post with dry rot?

What computer would be fastest for Mathematica Home Edition?



Why flipping is done on data from Big endian before storing in LE?



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 Results
Why I closed the “Why is Kali so hard” questionSize of data that can be written to / read from socketsPhysical layer header is not present in the data read from raw sockets?How to send data from serial port over wifi?Emulate console by sending data on one UDP port, and receive text from anotherHow to pass received data from netcat to another script as argument?Why doesn't file redirection to and from a named pipe work, but piping to cat does?When does the kernel cut UDP datagram into MTU chunks?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















Suppose A is a BE machine and is sending 0x44332211 sending data to B which is LE. By default A will be using htonl of 0x44332211 which is nothing but 0x44332211.



Now when 0x44332211 reaches B,B knows that its a BE format, so B will flip by using htonl function and change it to 0x11223344.



What does this flipping meeans? I read this concept from internet. Why flipping is required before storing?



Because in that case flipped value 0x11223344 will be stored in LE like 0x44332211,which is different from what A send because B interprets it differently though they look alike?



/* Conversion from BE to LE */










share|improve this question






























    0















    Suppose A is a BE machine and is sending 0x44332211 sending data to B which is LE. By default A will be using htonl of 0x44332211 which is nothing but 0x44332211.



    Now when 0x44332211 reaches B,B knows that its a BE format, so B will flip by using htonl function and change it to 0x11223344.



    What does this flipping meeans? I read this concept from internet. Why flipping is required before storing?



    Because in that case flipped value 0x11223344 will be stored in LE like 0x44332211,which is different from what A send because B interprets it differently though they look alike?



    /* Conversion from BE to LE */










    share|improve this question


























      0












      0








      0








      Suppose A is a BE machine and is sending 0x44332211 sending data to B which is LE. By default A will be using htonl of 0x44332211 which is nothing but 0x44332211.



      Now when 0x44332211 reaches B,B knows that its a BE format, so B will flip by using htonl function and change it to 0x11223344.



      What does this flipping meeans? I read this concept from internet. Why flipping is required before storing?



      Because in that case flipped value 0x11223344 will be stored in LE like 0x44332211,which is different from what A send because B interprets it differently though they look alike?



      /* Conversion from BE to LE */










      share|improve this question
















      Suppose A is a BE machine and is sending 0x44332211 sending data to B which is LE. By default A will be using htonl of 0x44332211 which is nothing but 0x44332211.



      Now when 0x44332211 reaches B,B knows that its a BE format, so B will flip by using htonl function and change it to 0x11223344.



      What does this flipping meeans? I read this concept from internet. Why flipping is required before storing?



      Because in that case flipped value 0x11223344 will be stored in LE like 0x44332211,which is different from what A send because B interprets it differently though they look alike?



      /* Conversion from BE to LE */







      socket






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 17 hours ago









      Rui F Ribeiro

      42.1k1483142




      42.1k1483142










      asked Apr 22 '13 at 17:22









      Subi SureshSubi Suresh

      1601312




      1601312




















          2 Answers
          2






          active

          oldest

          votes


















          2














          The htonl() and ntohl() functions in a big endian C library actually don't do anything (they are "no-op"s). This way the same code can be used on both BE and LE machines; compiled with the C lib on the LE machine, those functions will flip the bytes, but on the BE machine, they will leave them the same, since network byte order is big endian, and the host is big endian already.



          In other words, in your example, BE machine A never changes anything. Only the LE machine B does.






          share|improve this answer























          • Ya goldilocks.Why LE machine ie B flips the data received?Why cant it store as such.

            – Subi Suresh
            Apr 22 '13 at 17:51











          • Because it is little endian, so that is the way it stores data. If it doesn't do that, when that value is used subsequently, it will be wrong because the bytes are backward. Why there are the two styles is I guess a historical question...presumably big endian existed first en.wikipedia.org/wiki/Little_endian#History but little endian is a hardware level optimization.

            – goldilocks
            Apr 22 '13 at 17:56











          • > i am summarising from the inputs you have given.What ever the machine is always the data is stored as byte 0 1st,byte 2 second and so on.Since B is a LE,it needs to store in the reverse order of A.so we are using htons(), so that byte 0 will be 11 byte1 will be 22 ,byte2 will be 33 and byte 4 will 44.Am i right?no need to worry about the internal representations in LE and BE.

            – Subi Suresh
            Apr 22 '13 at 18:01












          • Yes, as long as you use htonl/ntohl before transmitting/after receiving, you don't have to worry about the internal representation. Even though most machines on the internet are probably LE, BE is network order to ensure consistent methodology. Remember, all this only applies to addresses used in IP packet headers -- the actual data in an IP transmission is structured according to whatever higher level protocols are used. So it's not everything, it's just specific bits (the address, the port number).

            – goldilocks
            Apr 22 '13 at 18:09


















          0














          Actually, flipping is not required before storage in a specific manner. It is required because the host uses the little endian order so when it receives a number longer than one byte from the network, it flips the bytes to interpret that number correctly. It is necessary doing this flipping before the data is used for whichever purpose it can be.



          As an analogy, imagine yourself reading a manga printed in the original japanese way (i.e. the page usually being the last being actually the first one). If you're not aware of this fact, and don't "flip" the book, you're just unable to understand the story.






          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%2f73333%2fwhy-flipping-is-done-on-data-from-big-endian-before-storing-in-le%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            The htonl() and ntohl() functions in a big endian C library actually don't do anything (they are "no-op"s). This way the same code can be used on both BE and LE machines; compiled with the C lib on the LE machine, those functions will flip the bytes, but on the BE machine, they will leave them the same, since network byte order is big endian, and the host is big endian already.



            In other words, in your example, BE machine A never changes anything. Only the LE machine B does.






            share|improve this answer























            • Ya goldilocks.Why LE machine ie B flips the data received?Why cant it store as such.

              – Subi Suresh
              Apr 22 '13 at 17:51











            • Because it is little endian, so that is the way it stores data. If it doesn't do that, when that value is used subsequently, it will be wrong because the bytes are backward. Why there are the two styles is I guess a historical question...presumably big endian existed first en.wikipedia.org/wiki/Little_endian#History but little endian is a hardware level optimization.

              – goldilocks
              Apr 22 '13 at 17:56











            • > i am summarising from the inputs you have given.What ever the machine is always the data is stored as byte 0 1st,byte 2 second and so on.Since B is a LE,it needs to store in the reverse order of A.so we are using htons(), so that byte 0 will be 11 byte1 will be 22 ,byte2 will be 33 and byte 4 will 44.Am i right?no need to worry about the internal representations in LE and BE.

              – Subi Suresh
              Apr 22 '13 at 18:01












            • Yes, as long as you use htonl/ntohl before transmitting/after receiving, you don't have to worry about the internal representation. Even though most machines on the internet are probably LE, BE is network order to ensure consistent methodology. Remember, all this only applies to addresses used in IP packet headers -- the actual data in an IP transmission is structured according to whatever higher level protocols are used. So it's not everything, it's just specific bits (the address, the port number).

              – goldilocks
              Apr 22 '13 at 18:09















            2














            The htonl() and ntohl() functions in a big endian C library actually don't do anything (they are "no-op"s). This way the same code can be used on both BE and LE machines; compiled with the C lib on the LE machine, those functions will flip the bytes, but on the BE machine, they will leave them the same, since network byte order is big endian, and the host is big endian already.



            In other words, in your example, BE machine A never changes anything. Only the LE machine B does.






            share|improve this answer























            • Ya goldilocks.Why LE machine ie B flips the data received?Why cant it store as such.

              – Subi Suresh
              Apr 22 '13 at 17:51











            • Because it is little endian, so that is the way it stores data. If it doesn't do that, when that value is used subsequently, it will be wrong because the bytes are backward. Why there are the two styles is I guess a historical question...presumably big endian existed first en.wikipedia.org/wiki/Little_endian#History but little endian is a hardware level optimization.

              – goldilocks
              Apr 22 '13 at 17:56











            • > i am summarising from the inputs you have given.What ever the machine is always the data is stored as byte 0 1st,byte 2 second and so on.Since B is a LE,it needs to store in the reverse order of A.so we are using htons(), so that byte 0 will be 11 byte1 will be 22 ,byte2 will be 33 and byte 4 will 44.Am i right?no need to worry about the internal representations in LE and BE.

              – Subi Suresh
              Apr 22 '13 at 18:01












            • Yes, as long as you use htonl/ntohl before transmitting/after receiving, you don't have to worry about the internal representation. Even though most machines on the internet are probably LE, BE is network order to ensure consistent methodology. Remember, all this only applies to addresses used in IP packet headers -- the actual data in an IP transmission is structured according to whatever higher level protocols are used. So it's not everything, it's just specific bits (the address, the port number).

              – goldilocks
              Apr 22 '13 at 18:09













            2












            2








            2







            The htonl() and ntohl() functions in a big endian C library actually don't do anything (they are "no-op"s). This way the same code can be used on both BE and LE machines; compiled with the C lib on the LE machine, those functions will flip the bytes, but on the BE machine, they will leave them the same, since network byte order is big endian, and the host is big endian already.



            In other words, in your example, BE machine A never changes anything. Only the LE machine B does.






            share|improve this answer













            The htonl() and ntohl() functions in a big endian C library actually don't do anything (they are "no-op"s). This way the same code can be used on both BE and LE machines; compiled with the C lib on the LE machine, those functions will flip the bytes, but on the BE machine, they will leave them the same, since network byte order is big endian, and the host is big endian already.



            In other words, in your example, BE machine A never changes anything. Only the LE machine B does.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Apr 22 '13 at 17:36









            goldilocksgoldilocks

            63.5k18157213




            63.5k18157213












            • Ya goldilocks.Why LE machine ie B flips the data received?Why cant it store as such.

              – Subi Suresh
              Apr 22 '13 at 17:51











            • Because it is little endian, so that is the way it stores data. If it doesn't do that, when that value is used subsequently, it will be wrong because the bytes are backward. Why there are the two styles is I guess a historical question...presumably big endian existed first en.wikipedia.org/wiki/Little_endian#History but little endian is a hardware level optimization.

              – goldilocks
              Apr 22 '13 at 17:56











            • > i am summarising from the inputs you have given.What ever the machine is always the data is stored as byte 0 1st,byte 2 second and so on.Since B is a LE,it needs to store in the reverse order of A.so we are using htons(), so that byte 0 will be 11 byte1 will be 22 ,byte2 will be 33 and byte 4 will 44.Am i right?no need to worry about the internal representations in LE and BE.

              – Subi Suresh
              Apr 22 '13 at 18:01












            • Yes, as long as you use htonl/ntohl before transmitting/after receiving, you don't have to worry about the internal representation. Even though most machines on the internet are probably LE, BE is network order to ensure consistent methodology. Remember, all this only applies to addresses used in IP packet headers -- the actual data in an IP transmission is structured according to whatever higher level protocols are used. So it's not everything, it's just specific bits (the address, the port number).

              – goldilocks
              Apr 22 '13 at 18:09

















            • Ya goldilocks.Why LE machine ie B flips the data received?Why cant it store as such.

              – Subi Suresh
              Apr 22 '13 at 17:51











            • Because it is little endian, so that is the way it stores data. If it doesn't do that, when that value is used subsequently, it will be wrong because the bytes are backward. Why there are the two styles is I guess a historical question...presumably big endian existed first en.wikipedia.org/wiki/Little_endian#History but little endian is a hardware level optimization.

              – goldilocks
              Apr 22 '13 at 17:56











            • > i am summarising from the inputs you have given.What ever the machine is always the data is stored as byte 0 1st,byte 2 second and so on.Since B is a LE,it needs to store in the reverse order of A.so we are using htons(), so that byte 0 will be 11 byte1 will be 22 ,byte2 will be 33 and byte 4 will 44.Am i right?no need to worry about the internal representations in LE and BE.

              – Subi Suresh
              Apr 22 '13 at 18:01












            • Yes, as long as you use htonl/ntohl before transmitting/after receiving, you don't have to worry about the internal representation. Even though most machines on the internet are probably LE, BE is network order to ensure consistent methodology. Remember, all this only applies to addresses used in IP packet headers -- the actual data in an IP transmission is structured according to whatever higher level protocols are used. So it's not everything, it's just specific bits (the address, the port number).

              – goldilocks
              Apr 22 '13 at 18:09
















            Ya goldilocks.Why LE machine ie B flips the data received?Why cant it store as such.

            – Subi Suresh
            Apr 22 '13 at 17:51





            Ya goldilocks.Why LE machine ie B flips the data received?Why cant it store as such.

            – Subi Suresh
            Apr 22 '13 at 17:51













            Because it is little endian, so that is the way it stores data. If it doesn't do that, when that value is used subsequently, it will be wrong because the bytes are backward. Why there are the two styles is I guess a historical question...presumably big endian existed first en.wikipedia.org/wiki/Little_endian#History but little endian is a hardware level optimization.

            – goldilocks
            Apr 22 '13 at 17:56





            Because it is little endian, so that is the way it stores data. If it doesn't do that, when that value is used subsequently, it will be wrong because the bytes are backward. Why there are the two styles is I guess a historical question...presumably big endian existed first en.wikipedia.org/wiki/Little_endian#History but little endian is a hardware level optimization.

            – goldilocks
            Apr 22 '13 at 17:56













            > i am summarising from the inputs you have given.What ever the machine is always the data is stored as byte 0 1st,byte 2 second and so on.Since B is a LE,it needs to store in the reverse order of A.so we are using htons(), so that byte 0 will be 11 byte1 will be 22 ,byte2 will be 33 and byte 4 will 44.Am i right?no need to worry about the internal representations in LE and BE.

            – Subi Suresh
            Apr 22 '13 at 18:01






            > i am summarising from the inputs you have given.What ever the machine is always the data is stored as byte 0 1st,byte 2 second and so on.Since B is a LE,it needs to store in the reverse order of A.so we are using htons(), so that byte 0 will be 11 byte1 will be 22 ,byte2 will be 33 and byte 4 will 44.Am i right?no need to worry about the internal representations in LE and BE.

            – Subi Suresh
            Apr 22 '13 at 18:01














            Yes, as long as you use htonl/ntohl before transmitting/after receiving, you don't have to worry about the internal representation. Even though most machines on the internet are probably LE, BE is network order to ensure consistent methodology. Remember, all this only applies to addresses used in IP packet headers -- the actual data in an IP transmission is structured according to whatever higher level protocols are used. So it's not everything, it's just specific bits (the address, the port number).

            – goldilocks
            Apr 22 '13 at 18:09





            Yes, as long as you use htonl/ntohl before transmitting/after receiving, you don't have to worry about the internal representation. Even though most machines on the internet are probably LE, BE is network order to ensure consistent methodology. Remember, all this only applies to addresses used in IP packet headers -- the actual data in an IP transmission is structured according to whatever higher level protocols are used. So it's not everything, it's just specific bits (the address, the port number).

            – goldilocks
            Apr 22 '13 at 18:09













            0














            Actually, flipping is not required before storage in a specific manner. It is required because the host uses the little endian order so when it receives a number longer than one byte from the network, it flips the bytes to interpret that number correctly. It is necessary doing this flipping before the data is used for whichever purpose it can be.



            As an analogy, imagine yourself reading a manga printed in the original japanese way (i.e. the page usually being the last being actually the first one). If you're not aware of this fact, and don't "flip" the book, you're just unable to understand the story.






            share|improve this answer



























              0














              Actually, flipping is not required before storage in a specific manner. It is required because the host uses the little endian order so when it receives a number longer than one byte from the network, it flips the bytes to interpret that number correctly. It is necessary doing this flipping before the data is used for whichever purpose it can be.



              As an analogy, imagine yourself reading a manga printed in the original japanese way (i.e. the page usually being the last being actually the first one). If you're not aware of this fact, and don't "flip" the book, you're just unable to understand the story.






              share|improve this answer

























                0












                0








                0







                Actually, flipping is not required before storage in a specific manner. It is required because the host uses the little endian order so when it receives a number longer than one byte from the network, it flips the bytes to interpret that number correctly. It is necessary doing this flipping before the data is used for whichever purpose it can be.



                As an analogy, imagine yourself reading a manga printed in the original japanese way (i.e. the page usually being the last being actually the first one). If you're not aware of this fact, and don't "flip" the book, you're just unable to understand the story.






                share|improve this answer













                Actually, flipping is not required before storage in a specific manner. It is required because the host uses the little endian order so when it receives a number longer than one byte from the network, it flips the bytes to interpret that number correctly. It is necessary doing this flipping before the data is used for whichever purpose it can be.



                As an analogy, imagine yourself reading a manga printed in the original japanese way (i.e. the page usually being the last being actually the first one). If you're not aware of this fact, and don't "flip" the book, you're just unable to understand the story.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 22 '13 at 17:42









                lgeorgetlgeorget

                9,13622754




                9,13622754



























                    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%2f73333%2fwhy-flipping-is-done-on-data-from-big-endian-before-storing-in-le%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







                    -socket

                    Popular posts from this blog

                    Frič See also Navigation menuinternal link

                    Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

                    fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox