Flip from little endian to big endian ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dissectcode
    New Member
    • Jul 2008
    • 66

    Flip from little endian to big endian ?

    Is there a quick method to flip endian-ness in C? Different platforms I am using, use big and little endian, and I want to flip everything to big.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Why?

    To expand on my question if you mean you want to use big endian for all data storage no matter what platform is used then that is a mistake. You should let the platform work in it's native endian, anything else will be atrociously in-efficient.

    The only things that should have fixed endian are those things that are truely platform independent, that is communication protocols, file formats and similar methods of data transfer.

    You can then easily write functions that read and write data out of the file (or protocol etc.) as bytes and combines those bytes into integers etc in a portible manor using the bitwise operators (shifts, ors and ands).

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      There are already readymade functions for this like ntoh,hton etc.

      Raghu

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Like I have just said elsewhere ntoh, hton are part of a third party library and are not available on every platform and therefore are inherently non-portable.

        The portable method is to use the bitwise operators.

        Comment

        • dissectcode
          New Member
          • Jul 2008
          • 66

          #5
          Why?

          Because when we display results from an address, they are reversed, depending on the platform.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            A memory address? An IP address? You are not explaining your problem fully.

            Do these platforms communicate with each other or are they sealed pieces of software, i.e. do you have problem of sending data between platforms with different endians or have you managed to write non-portable software and then transferred it to a platform that it doesn't work properly on?

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              Originally posted by dissectcode
              Why?
              Because when we display results from an address, they are reversed, depending on the platform.
              It sounds like you're describing some sort of debugger: you type in an address and it prints out the contents of that address. If so, then I don't see why you need to worry about endianness -- simply cast the address to the appropriate pointer type and dereference the pointer.

              In my experience, the only times when I needed to worry about endianness were:

              1. Reading or writing a data file that might be used by many platforms. In this case it was important to specify the endianness of the data file so each platform could properly access it.

              2. Reading or writing a message stream that might be used by many platforms. In this case it was important to specify the endianness of the message so each platform could properly access it.

              3. PCI accesses. The PCI standard specifies the endianness of the memory-mapped I/O, so the platform has comply with that endianness even if it does't match the native endianness of the processor.

              4. Interpreting panic dump printouts. [Now I'm showing my age -- does anybody still have to deal with these?]

              The common thread here is that endianness only needs to be explicitly taken into account when information is passed between platforms that might have different native endianness.

              Comment

              Working...