Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Byte Swapping Big End to Little End

Byte Swapping Big End to Little End

Scheduled Pinned Locked Moved C / C++ / MFC
testingbeta-testing
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Grahamfff
    wrote on last edited by
    #1

    I am receiving a data structure from a Unix machine (Big Endian) which contains doubles, integerss and shorts. Using UDP for the transmission of the data. Now what do I need to do at the Windows end to access this data correctley; i.e. adouble with the value 55.5 on Unix having the same value on Windows; Can I do byte swapping on the data structure received (this must have been done before); or do you need to byte swap each type individually; e.g. to get the double and the 32 bit integers and the 16 bit shorts. At present I will assume byte aligned but will check during testing and avoid booleans. Regards, Andy.

    grahamfff

    C L _ 3 Replies Last reply
    0
    • G Grahamfff

      I am receiving a data structure from a Unix machine (Big Endian) which contains doubles, integerss and shorts. Using UDP for the transmission of the data. Now what do I need to do at the Windows end to access this data correctley; i.e. adouble with the value 55.5 on Unix having the same value on Windows; Can I do byte swapping on the data structure received (this must have been done before); or do you need to byte swap each type individually; e.g. to get the double and the 32 bit integers and the 16 bit shorts. At present I will assume byte aligned but will check during testing and avoid booleans. Regards, Andy.

      grahamfff

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      you have to swap on the individual items (not on the struct).

      image processing toolkits | batch image processing

      1 Reply Last reply
      0
      • G Grahamfff

        I am receiving a data structure from a Unix machine (Big Endian) which contains doubles, integerss and shorts. Using UDP for the transmission of the data. Now what do I need to do at the Windows end to access this data correctley; i.e. adouble with the value 55.5 on Unix having the same value on Windows; Can I do byte swapping on the data structure received (this must have been done before); or do you need to byte swap each type individually; e.g. to get the double and the 32 bit integers and the 16 bit shorts. At present I will assume byte aligned but will check during testing and avoid booleans. Regards, Andy.

        grahamfff

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        As Chris said, you do need type-specific code. In particular you need byte swapping only when multi-byte data is involved, so a 4-byte int needs its 4 bytes swapped; an 8-byte double needs its 8 bytes swapped; and a 4-byte byte array is fine the way it is. Just make a drawing of what the data looks like on a big-endian machine, and then again on a little-endian machine, and you'll never forget what is involved. BTW: float values could use different encodings altogether; nowadays most systems use an IEEE format, however I have seen some other schemes over the years. When different, it becomes really hard to get them across, and you might want to consider a different representation, maybe even a string! :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        1 Reply Last reply
        0
        • G Grahamfff

          I am receiving a data structure from a Unix machine (Big Endian) which contains doubles, integerss and shorts. Using UDP for the transmission of the data. Now what do I need to do at the Windows end to access this data correctley; i.e. adouble with the value 55.5 on Unix having the same value on Windows; Can I do byte swapping on the data structure received (this must have been done before); or do you need to byte swap each type individually; e.g. to get the double and the 32 bit integers and the 16 bit shorts. At present I will assume byte aligned but will check during testing and avoid booleans. Regards, Andy.

          grahamfff

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          As the other answers suggested, you have to do this for individual elements of the structure. Here is an article on endian-ness - Basic concepts on Endianness[^]

          «_Superman_» I love work. It gives me something to do between weekends.

          G 1 Reply Last reply
          0
          • _ _Superman_

            As the other answers suggested, you have to do this for individual elements of the structure. Here is an article on endian-ness - Basic concepts on Endianness[^]

            «_Superman_» I love work. It gives me something to do between weekends.

            G Offline
            G Offline
            Grahamfff
            wrote on last edited by
            #5

            Thanks for the posts, I did try the following code, but am I on the right track as the float did not work.

            struct RXData {
            double timeStamp;
            double item1;
            long item2;
            double item3;
            int flag1;
            bool state;
            }

            float swap(float d)
            {
            float a;
            unsigned char *dst = (unsigned char *)&a;
            unsigned char *src = (unsigned char *)&d;
            dst[0] = src[3];
            dst[1] = src[2];
            dst[2] = src[1];
            dst[3] = src[0];
            return a;
            }

            short byteSwap16(short value)
            {
            short newValue = 0;
            char* pnewValue = (char*) &newValue;
            char* poldValue = (char*) &value;

            pnewValue[0] = poldValue[1];
            pnewValue[1] = poldValue[0];

            return newValue;
            }

            unsigned long byteSwap32(unsigned long value)
            {
            unsigned long newValue = 0;
            char* pnewValue = (char *) &newValue;
            char* poldValue = (char *) &value;

            pnewValue[0] = poldValue[3];
            pnewValue[1] = poldValue[2];
            pnewValue[2] = poldValue[1];
            pnewValue[3] = poldValue[0];

            return newValue ;
            }

            RxData.timeStamp = swap(RxData.timeStamp

            );

            grahamfff

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups