• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: "byte orders" question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "byte orders" question


  • Subject: Re: "byte orders" question
  • From: Scott Ribe <email@hidden>
  • Date: Sun, 27 Nov 2011 08:02:00 -0700

On Nov 27, 2011, at 6:06 AM, Koen van der Drift wrote:

> Again giving the same expected results.  Would this be the correct call, or am I still missing something?

The purpose of casting? I think you need to review a C reference, you're casting a uint32_t to uint32_t * then back to uint32_t. In prior code you were casting something (unsigned char * ???) to uint32_t *, then dereferencing it, which would be a uint32_t, then casting that to a uint32_t.

NSData *base64DecodedData = [NSData dataFromBase64String: @"Q5YIjESWO5JDlpIbRzMVL0OW="];
int n = [NSData length] / 4;
const uint32_t *buf = (const uint32_t *) [NSData bytes];
float *results = (float *) malloc(n * 4);
for( int i = 0; i < n; ++i )
	((uint32_t *)results)[n] = ntohl( buf[n] );

Two of the casts, of bytes & malloc results, are not necessary in plain C, but they're required in C++ and are habit for me ;-)

Of course you might want to check that sizeof( float ) is 4, but the prior suggestion to use it in the code in place of 4 is off, because your input will still be 4 bytes I presume, and if your platform float is not 4, then you can't make it work and have to fail. (Of course, if your platform doesn't adhere to IEE floating point standards, and your input does, or vice versa, you're screwed anyway.) You might want to check that the data length is an exact multiple of 4, or rather 8 since you're expecting floats in pairs. And so on...

--
Scott Ribe
email@hidden
http://www.elevated-dev.com/
(303) 722-0567 voice




_______________________________________________

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

  • Follow-Ups:
    • Re: "byte orders" question
      • From: Koen van der Drift <email@hidden>
References: 
 >"byte orders" question (From: Koen van der Drift <email@hidden>)
 >Re: "byte orders" question (From: Ben Kennedy <email@hidden>)
 >Re: "byte orders" question (From: Koen van der Drift <email@hidden>)
 >Re: "byte orders" question (From: Scott Ribe <email@hidden>)
 >Re: "byte orders" question (From: Koen van der Drift <email@hidden>)
 >Re: "byte orders" question (From: "Glenn L. Austin" <email@hidden>)
 >Re: "byte orders" question (From: Koen van der Drift <email@hidden>)
 >Re: "byte orders" question (From: Scott Ribe <email@hidden>)
 >Re: "byte orders" question (From: Koen van der Drift <email@hidden>)
 >Re: "byte orders" question (From: Scott Ribe <email@hidden>)
 >Re: "byte orders" question (From: Koen van der Drift <email@hidden>)

  • Prev by Date: MapKit
  • Next by Date: Re: NSView mouseDown truncated coordinates
  • Previous by thread: Re: "byte orders" question
  • Next by thread: Re: "byte orders" question
  • Index(es):
    • Date
    • Thread