I have UTF-8 encoded NSData
from windows server and I want to convert it to NSString
for iPhone. Since data contains characters (like a degree symbol) which have different values on both platforms, how do I convert data to string?
Ios – Convert UTF-8 encoded NSData to NSString
iosnsdatansstring
Best Solution
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.(Note that if the input is not properly UTF-8-encoded, you will get
nil
.)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.