How do I convert an NSString value to NSData?

如何将NSString值转换为NSData


NSString* str = @"teststring";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];

NSString *str = @"helowrld";
// This converts the string to an NSData object
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

你可以参考这个链接


Converting NSString like so will force a trailing byte, which means NSJSONSerialization:JSONObjectWithData and others will fail unless you remove it.

Ie do

NSData* data = [yourString dataUsingEncoding:NSUTF8StringEncoding];

and then

data = [data subdataWithRange:NSMakeRange(0, [data length] - 1)];

then feel free to proceed with NSJSONSerialization:JSONObjectWithData .

链接地址: http://www.djcxy.com/p/35130.html

上一篇: 对iPhone上的NSString进行AES加密

下一篇: 如何将NSString值转换为NSData?