Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert NSString to NSData?

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.

like image 342
Steve McLeod Avatar asked Aug 17 '09 11:08

Steve McLeod


1 Answers

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.

like image 92
Peter Hosey Avatar answered Sep 30 '22 22:09

Peter Hosey