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



On Jun 7, 2006, at 7:14 PM, David Alger wrote:

Thanks Steve,

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

Well, it was written off the top of my head in Mail, so be sure to validate everything yourself.


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

I had to do a bit for one project recently since some data was loaded in from resources that have multi-byte fields stored as big-endian. The reader/writers localize all the byte swapping into just a few places so the bulk of the code just works with native-endian data structures. It also means that I don't need to have a bunch of conditional code all over the place that I need to keep track of.


steve


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/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>)
 >Re: Swapping variables of type float (From: David Alger <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.