ios 如何将 NSData 转换为 NSString?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10565666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:21:22  来源:igfitidea点击:

How to convert NSData to NSString?

objective-ciosxcode

提问by tna0y

Possible Duplicate:
Convert UTF-8 encoded NSData to NSString

可能的重复:
将 UTF-8 编码的 NSData 转换为 NSString

I need to to convert the NSData read from file to NSString. How to do it?

我需要将从文件中读取的 NSData 转换为 NSString。怎么做?

回答by modocache

NSStringprovides an initializer for this purpose.

NSString为此目的提供了一个初始化程序。

// NSData *data = [NSData data];
NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];

回答by Abhishek Singh

You can do it this way

你可以这样做

NSString *yourStr= [[[NSString alloc] initWithData:data
                                         encoding:NSUTF8StringEncoding] autorelease];

or the ohther way to use it when the data ends with a null is:-

或者当数据以 null 结尾时使用它的另一种方法是:-

NSString *yourStr= [NSString stringWithUTF8String:[theData bytes]];

回答by Tomas Camin

NSString *convertedString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]