Re: Non-nil terminated C string to NSString
Re: Non-nil terminated C string to NSString
- Subject: Re: Non-nil terminated C string to NSString
- From: Ryan Britton <email@hidden>
- Date: Wed, 2 Aug 2006 19:37:50 -0700
I don't know if this is the best way to do it, but this is how I
always have.
char *buffer = (char *) malloc((passwordLength + 1) * sizeof(char));
//Put it in a zero-terminated buffer so stringWithUTF8String can
handle it
strncpy(buffer, passwordData, passwordLength);
buffer[passwordLength] = 0;
NSString *password = [NSString stringWithUTF8String:buffer];
free(buffer);
On Aug 2, 2006, at 6:00 PM, Ben Phipps wrote:
I want to turn a non-nil terminated c string (such as the one
received from Keychain) into an NSString. Since
stringWithCString:length: is deprecated in 10.4, what is the
correct way to do this?
Creating an NSString then asking for a substring seems wasteful as
the initial string could be huge. And inserting a null character
into the c string doesn't work since I could be corrupting memory.
Any suggestions?
Thanks,
Ben
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden