Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Swapping variables of type float



Thanks Steve,

The code sample you gave looks great and I'll put it to good use.

I had thought that this was the case but wasn't quite sure, as I have never dealt with endian issues before.

Sincerely,
David Alger

Family Friendly Software, LLC
http://www.familyfriendlysoftware.com



On Jun 7, 2006, at 8:44 PM, Steve Christensen wrote:

On Jun 7, 2006, at 6:00 PM, David Alger wrote:

I have some structs that I write/read to and from disk, and they have > 1 byte variables that need to be swapped.

My question is would you swap a type 'float' var just as you would a UInt16?

Yes, since the byte ordering is likewise reversed. You just have to be careful that an unexpected type conversion doesn't occur when copying the float into a UInt32 so that it can be swapped. Maybe do something like this?


inline float SwapBigEndianFloatToHost(float unswappedFloat)
{
#if defined(__BIG_ENDIAN__)

    return unswappedFloat;

#else

    union
    {
        float   fFloat;
        UInt32  fFloatAsUInt32;
    } swapper;

swapper.fFloat = unswappedFloat;
swapper.fFloatAsUInt32 = CFSwapInt32BigToHost (swapper.fFloatAsUInt32);


    return swapper.fFloat;

#endif
}

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Carbon-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/carbon-dev/davidalger% 40millertel.net


This email sent to email@hidden


_______________________________________________ Do not post admin requests to the list. They will be ignored. Carbon-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/carbon-dev/email@hidden

This email sent to email@hidden
References: 
 >Swapping variables of type float (From: David Alger <email@hidden>)
 >Re: Swapping variables of type float (From: Steve Christensen <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.