Re: How to convert UInt8 array to NSString
Re: How to convert UInt8 array to NSString
- Subject: Re: How to convert UInt8 array to NSString
- From: Jens Alfke <email@hidden>
- Date: Mon, 5 May 2008 09:20:40 -0700
On 5 May '08, at 5:17 AM, Marc Lohse wrote:
A C function that i'd like to use returns an array
of UInt8 and i simply don't find out how to convert
this into a NSString...
It depends on what the encoding of the C string is. In the general
case, use
[NSString stringWithCString: ... encoding: ...]
where the encoding constants are listed near the top of NSString.h.
Typical values would be NSASCIIStringEncoding,
NSISOLatin1StringEncoding, NSWindowsCP1252StringEncoding. (Be warned
that if you use ASCII, the NSString method will fail and return nil if
it finds any non-ASCII characters, i.e. anything > 0x7F. I find it
safest to go with the Windows encoding, because it's a superset of
both ASCII and ISO-Latin-1 that maps all 256 characters, so it never
returns nil. It's also the default encoding on Windows, so there's a
lot of text out there in the wild encoded with it.)
If it's UTF-8 you can use the shortcut [NSString stringWithUTF8String:].
But if it's literally just an array of UInt8 without a NUL byte at the
end, you'll need to use
[[NSString alloc] initWithBytes: length: encoding].
(There's no autoreleased equivalent of that call.)
—Jens
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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