Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Converting NSData* to primitive type (short, int double...)

Converting NSData* to primitive type (short, int double...)
Thread Tools
Fresh-Faced Recruit
Join Date: Jul 2002
Status: Offline
Reply With Quote
Jul 30, 2002, 01:47 AM
 
I'm new to Cocoa/Objective-C and am trying to port some of my existing applications to the new platform. Unfortunately I'm having trouble writing filters to parse existing file formats.

How can I convert an NSData*, read from a binary file, into a Cocoa primitive data type? For instance, I'd like to convert a NSData* containing 2 bytes into a short and 8 bytes into a double.

Are there techniques to address endian issues?

Cheers
     
Mac Elite
Join Date: May 2002
Status: Offline
Reply With Quote
Jul 30, 2002, 04:25 AM
 
The bytes method returns a pointer to the NSData's contents. So what you do is typecast that pointer to be a pointer to the type you want, and copy it into a variable.. or even just leave it as it is. Example:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">short tempShort;
NSData *tempData = [object methodThatReturnsAnNSData];
tempShort = &amp;(short *)[tempData bytes];</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">
[vash:~] banana% killall killall
Terminated
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 30, 2002, 06:22 AM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by frogstar:
<strong>Are there techniques to address endian issues?</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Foundation includes functions for converting between endian formats. They're listed in the documentation under "Byte Ordering."
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Junior Member
Join Date: Mar 2001
Status: Offline
Reply With Quote
Jul 30, 2002, 03:28 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Gul Banana:
<strong>The bytes method returns a pointer to the NSData's contents. So what you do is typecast that pointer to be a pointer to the type you want, and copy it into a variable.. or even just leave it as it is. Example:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">short tempShort;
NSData *tempData = [object methodThatReturnsAnNSData];
tempShort = &amp;(short *)[tempData bytes];</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif"></strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Gul Banana's code won't compile (he means * instead of & and even if it did, it's not any good because of alignment issues.

Do this instead.

short tempShort;
[tempData getBytes:&tempShort];

Of course, only do this if you're sure that tempData is two bytes long.

-Peter
     
Fresh-Faced Recruit
Join Date: Jul 2002
Status: Offline
Reply With Quote
Jul 30, 2002, 04:02 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif"><strong>short tempShort;
[tempData getBytes:&tempShort];
</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Thanks for your help. This worked great!
     
   
Thread Tools
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -5. The time now is 11:16 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2