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]
本文详细介绍了如何将UTF-8编码的数据转换为Objective-C中的NSString对象,包括null-terminated数据和非null-terminated数据两种情况。通过使用分配、初始化和字符串方法,确保了转换过程的正确性和效率。
1万+

被折叠的 条评论
为什么被折叠?



