Convert UTF-8 encoded NSData to NSString

Posted by Editorial Staff | Updated on

If the data is not null-terminated, you should use -initWithData:encoding:

If the data is null-terminated, you should instead use -stringWithUTF8String: to avoid the extra \0 at the end.

Swift variant:

If the data is null-terminated, you could go though the safe way which is remove the that null character, or the unsafe way similar to the Objective-C version above.


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers