If the data is not null-terminated, you should use -initWithData:encoding:
NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
If the data is null-terminated, you should instead use -stringWithUTF8String: to avoid the extra \0 at the end.
NSString* newStr = [NSString stringWithUTF8String:[theData bytes]];
转载地址:[url]http://stackoverflow.com/questions/2467844/convert-utf-8-encoded-nsdata-to-nsstring/2467856#2467856[/url]
NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
If the data is null-terminated, you should instead use -stringWithUTF8String: to avoid the extra \0 at the end.
NSString* newStr = [NSString stringWithUTF8String:[theData bytes]];
转载地址:[url]http://stackoverflow.com/questions/2467844/convert-utf-8-encoded-nsdata-to-nsstring/2467856#2467856[/url]