On Jan 31, 2010, at 9:53 PM, Jens Alfke wrote:
> No. You don't want to pass a null terminated string to anything but the
> methods that explicitly take a C string. Anything else just wants the
> characters, with no nulls.
>
> --Jens {via iPhone}
NSString's +stringWithCString:encoding: does require a NULL terminated C string
[1].
My code worked fine with NSString's +stringWithCString:length: even though the
string was not NULL terminated. When I changed that to
+stringWithCString:encoding: I would *sometimes* get incorrect results. When I
added the code to copy the suppled non-null terminated string to a new buffer
with a null termination (prior to calling +stringWithCString:encoding:), the
results were made proper again.
[1] from the NSString documentation
<http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/clm/NSString/stringWithCString:encoding:>
+ (id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc
Parameters
cString: A C array of bytes. The array must end with a NULL character;
intermediate NULL characters are not allowed.
>
> On Jan 31, 2010, at 6:28 PM, Stuart Malin <[email protected]> wrote:
>
>>
>> On Jan 31, 2010, at 9:12 PM, Kyle Sluder wrote:
>>
>>> On Sun, Jan 31, 2010 at 5:51 PM, Stuart Malin <[email protected]> wrote:
>>>> I acquire a password from a keychain using SecKeychainFindGenericPassword.
>>>> That provides a non null terminated c string and a length. I had been
>>>> using [NSString stringWithCString: length:] to get an NSString instance of
>>>> the password. However, the +stringWithCString:length: method was
>>>> deprecated way back, so I thought I'd clean up my code and use
>>>> +stringWithCString:encoding: -- however that doesn't work because the
>>>> supplied C string isn't null terminated. I did a bit of searching, and
>>>> found in the archives on CocoaBuilder [1] someone's solution back in
>>>> September of 2005, which was to copy the acquired password to a new buffer
>>>> of 1 additional byte and create a new null terminated C string there:
>>>
>>> You could wrap the provided buffer with an NSData and use -[NSString
>>> initWithData:encoding:].
>>
>>
>> If I use +dataWithBytes:length: then I need to specify a length 1 greater,
>> and that additional byte must be set to 0 (to null terminate the string).
>> So, I could use NSMutableData, then -resetBytesInRange: to zero out the last
>> byte, which requires creating an NSRange struct, and that all seems heavier
>> than just using the C code. But then, the processor is fast, and this is
>> code that is run rarely, so optimizing isn't important. I guess it is just
>> a question of style...
>>
_______________________________________________
Cocoa-dev mailing list ([email protected])
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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]