Re: Sign extension in NSNumber / 10.4 SDK???
Re: Sign extension in NSNumber / 10.4 SDK???
- Subject: Re: Sign extension in NSNumber / 10.4 SDK???
- From: "Sean McBride" <email@hidden>
- Date: Thu, 17 Jul 2008 17:09:42 -0400
- Organization: Rogue Research
On 7/17/08 12:01 PM, Jens Alfke said:
>> Interesting. What about NSKeyedArchiver? It has, for example,
>> encodeInt32:forKey: but no unsigned equivalent. What should one do if
>> one wants to encode a uint32? Are there sign extension dangers here?
>
>It shouldn't be a problem as long as you call the matching decode
>method (-decodeInt32ForKey:). You'll get back the same 32 bits you put
>in. However, it could cause trouble if you read the archive using -
>decodeInt64ForKey:, and assign the result to a 64-bit int, because in
>that case you'll definitely get negative numbers out when you put in
>large UInt32s with the high bit set.
Indeed, I've just confirmed this:
uint32_t input = 0xFFFFFFFF; // 4294967295
NSMutableData* data = [NSMutableData data];
NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc]
initForWritingWithMutableData:data];
[archiver encodeInt32:(int32_t)input forKey:@"test"];
[archiver finishEncoding];
NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc]
initForReadingWithData:data];
int64_t output = [unarchiver decodeInt64ForKey:@"test"];
[unarchiver finishDecoding];
The value of output is _not_ 4294967295, it is -1.
I wonder why they added "encodeInt32:forKey:" but not an unsigned version...
--
____________________________________________________________
Sean McBride, B. Eng email@hidden
Rogue Research www.rogue-research.com
Mac Software Developer Montréal, Québec, Canada
_______________________________________________
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