 |
 |
Converting NSData* to primitive type (short, int double...)
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jul 2002
Status:
Offline
|
|
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
|
|
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 = &(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
|
|
</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
|
|
</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 = &(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
|
|
</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!
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|