Re: "byte orders" question
Re: "byte orders" question
- Subject: Re: "byte orders" question
- From: Nick Zitzmann <email@hidden>
- Date: Fri, 25 Nov 2011 14:23:46 -0700
On Nov 25, 2011, at 1:58 PM, Koen van der Drift wrote:
> I'm trying to implement the following code into my Cocoa project:
>
> for (n = 0 ; n < (2 * count) ; n++)
> {
> ((u_int32_t *) result)[n] = ntohl((u_int32_t) ((u_int32_t *) decoded)[n]);
> }
>
> with the following requirements:
>
> // byte order correction decoded has network byte order data
> // result has the byte order of the host machine
>
> decoded is an NSData object after base64 decoding.
>
> How do I obtain the "network byte order data" and "byte order of the host machine" so I get the correct results?
Network byte order data is always big-endian. The byte order of the host machine varies depending on the CPU architecture. The preprocessor macro __BIG_ENDIAN__ is always defined on big-endian architectures (e.g. PowerPC), and __LITTLE_ENDIAN__ on little-endian architectures (e.g. X86), but I think what you want to do here is just use the ntoh family of functions as you are doing above, because they will return the correct byte order for the host machine. IOW, they just return the number passed as arguments to them on big-endian CPUs, and they return a reversed byte order number on little-endian CPUs.
Nick Zitzmann
<http://www.chronosnet.com/>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden