Byte Swapping Floats

This is a discussion on Byte Swapping Floats within the C++ Programming forums, part of the General Programming Boards category; How do I go about swapping bytes (little endian -> big endian and visa-versa) in a platform independent way. I ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    5

    Byte Swapping Floats

    How do I go about swapping bytes (little endian -> big endian and visa-versa) in a platform independent way.

    I saw this:

    Code:
    float FloatSwap( float f )
    {
      union
      {
        float f;
        unsigned char b[4];
      } dat1, dat2;
    
      dat1.f = f;
      dat2.b[0] = dat1.b[3];
      dat2.b[1] = dat1.b[2];
      dat2.b[2] = dat1.b[1];
      dat2.b[3] = dat1.b[0];
      return dat2.f;
    }
    However apparently there are some situations where it causes problems:

    double swap(double) Doesn't Work

    I cant seem to get the correct values from the above function anyway. The dat2.f returned is always 0.0

    Many Thanks,
    -Alex

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by alexcurtis View Post
    I cant seem to get the correct values from the above function anyway. The dat2.f returned is always 0.0
    Why would you expect otherwise? By byte-swapping the value you've totally ruined it.

    The function should return an unsigned int (or more specifically, some integer type which is 32 bits in size), not a float. And you should not attempt to interpret the value as a float.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    5
    Quote Originally Posted by brewbuck View Post
    Why would you expect otherwise? By byte-swapping the value you've totally ruined it.

    The function should return an unsigned int (or more specifically, some integer type which is 32 bits in size), not a float. And you should not attempt to interpret the value as a float.
    OK. No Problem.

    Am I still going to run into problems on machines (with a different endian than the machine that wrote the file) when they try to decode it?

    -Alex

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by alexcurtis View Post
    OK. No Problem.

    Am I still going to run into problems on machines (with a different endian than the machine that wrote the file) when they try to decode it?

    -Alex
    Only if the two architectures use the same size and type of floats. If you want it really portable you should communicate the floating point integers in another format. Simple text is quite common and can be written and read directly using streams.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    5
    Quote Originally Posted by EVOEx View Post
    Only if the two architectures use the same size and type of floats. If you want it really portable you should communicate the floating point integers in another format. Simple text is quite common and can be written and read directly using streams.
    Im afraid I can't do that. Im writing to a specific file format that is binary and big-endian.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Something like this?
    Code:
    #include <stdio.h>
    
    void swapbytes(void *object, size_t size)
    {
       unsigned char *start, *end;
       for ( start = object, end = start + size - 1; start < end; ++start, --end )
       {
          unsigned char swap = *start;
          *start = *end;
          *end = swap;
       }
    }
    
    void showbytes(const void *object, size_t size)
    {
       unsigned char *byte = object;
       size_t i;
       for ( i = 0; i < size; ++i )
       {
          printf("%02X", (unsigned)*byte++);
       }
       putchar('\n');
    }
    
    int main()
    {
       unsigned long value = 0x12345678UL;
       showbytes(&value, sizeof value);
       swapbytes(&value, sizeof value);
       showbytes(&value, sizeof value);
       return 0;
    }
    
    /* my output
    78563412
    12345678
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by alexcurtis View Post
    Im afraid I can't do that. Im writing to a specific file format that is binary and big-endian.
    Then you should probably decode the floating point number (get the mantissa, exponent and sign) and write that to the binary file. Unless the file format doesn't have to be portable.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by EVOEx View Post
    Then you should probably decode the floating point number (get the mantissa, exponent and sign) and write that to the binary file. Unless the file format doesn't have to be portable.
    "specific file format" seems to imply the format is pre-defined.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by brewbuck View Post
    "specific file format" seems to imply the format is pre-defined.
    Oops, I misread it. I stand corrected.
    Then you may have to decode it on certain architectures and save it in the architecture's representation for floating point numbers. Again, if it needs portability to such systems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Need some help regarding data structures
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-28-2006, 04:19 AM
  4. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 04:46 PM
  5. sorting a structure of arrays
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 03-15-2002, 10:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21