views:

2054

answers:

1

I have this line of code to convert NSString to NSData:

NSData *data = [NSData dataWithBytes:[message UTF8String] length:[message lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];

How do I do this in Unicode instead of UTF8? My message may contain cyrillic characters or diacritical marks.

+5  A: 

First off, you should use dataUsingEncoding: instead of going through UTF8String. You only use UTF8String when you need a C string in that encoding.

Then, for “Unicode” (specifically, UTF-16), just pass NSUnicodeStringEncoding instead of NSUTF8StringEncoding in your dataUsingEncoding: message.

Peter Hosey
thanks! I somehow missed dataUsingEncoding in the docs.
William Denniss