Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-23-2010, 12:04 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 97
Question double to NSData?

How can I convert a double value to a NSData object?

Thanks!
SteveMobs is offline   Reply With Quote
Old 02-23-2010, 02:02 PM   #2 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 580
Default

To store a primitive type in an NSData object, you just need a pointer to the first byte of the value and the number of bytes:
Code:
double aDouble = 10.0;
NSData doubleData = [NSData dataWithBytes:&aDouble length:sizeof(double)];
You can get it back out like this:
Code:
double anotherDouble;
[doubleData getBytes:&anotherDouble length:sizeof(double)];
ChrisL is offline   Reply With Quote
Old 02-24-2010, 04:37 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 97
Thumbs up

Thank you! By the way... meanwhile I also found out, how to convert a double to an unsigned char array:
Code:
double aDouble = 10.0;
unsigned char doubleChars[sizeof(double)];
memcpy(doubleChars, &aDouble, sizeof(double));
SteveMobs is offline   Reply With Quote
Old 02-24-2010, 12:06 PM   #4 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 97
Default

Another quiz: Is it possible to convert just the second half of a double to NSData?

I tried this, which does not work:
Code:
double aDouble = 10.0;
NSData doubleData = [NSData dataWithBytes:(&aDouble)+sizeof(double)/2 length:sizeof(double)/2];
SteveMobs is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 241
15 members and 226 guests
ADY, Alsahir, cacao, dacapo, Dani77, Desert Diva, djohnson, HemiMG, jansan, M@realobjects, MarkC, prchn4christ, smethorst, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,762
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:55 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0