如何将NSString值转换为NSData?


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

Do:

NSData *data = [yourString dataUsingEncoding:NSUTF8StringEncoding];

然后继续NSJSONSerialization:JSONObjectWithData。


修正关于NULL结束符的答案

根据评论、官方文档和验证,更新了关于删除所谓NULL结束符的答案:

正如dataUsingEncoding所记录的:: 返回值 调用dataUsingEncoding:allowLossyConversion:的结果,NO作为第二个参数 正如getCString:maxLength:encoding:和cStringUsingEncoding::所记录的那样 注意dataUsingEncoding:allowLossyConversion:返回的数据不是一个严格的C-string,因为它没有NULL结束符


首先,你应该使用dataUsingEncoding:而不是通过UTF8String。只有在需要该编码的C字符串时才使用UTF8String。

然后,对于UTF-16,在你的dataUsingEncoding:消息中传递NSUnicodeStringEncoding而不是NSUTF8StringEncoding。


以防Swift开发者来这里,

将NSString / String转换为NSData

var _nsdata = _nsstring.dataUsingEncoding(NSUTF8StringEncoding)

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

你可以从这个链接得到参考


NSString *str = @"hello";
NSData *data = [NSData dataWithBytes:str.UTF8String length:str.length];

objective - c:

NSString *str = @"test string";
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:str];
NSString *thatStr = [NSKeyedUnarchiver unarchiveObjectWithData:data];

迅速:

let str = "test string"
let data = NSKeyedArchiver.archivedData(withRootObject: str)
let thatStr = NSKeyedUnarchiver.unarchiveObject(with: data) as! String

对于Swift 3,你将主要从字符串转换为数据。

let myString = "test"
let myData = myString.data(using: .utf8)
print(myData) // Optional(Data)

NSString *str = @"Banana";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:true];

objective - c

NSString *str = @"Hello World";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];

斯威夫特

let str = "Hello World"
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: false)

objective - c:

NSString to NSData:

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

NSData to NSString:

NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];

迅速:

字符串到数据:

var testString = "string"
var somedata = testString.data(using: String.Encoding.utf8)

数据到字符串:

var backToString = String(data: somedata!, encoding: String.Encoding.utf8) as String!

更新Swift 5.x

let str = "teststring"
let data = str.data(using: .utf8)

在Swift中,有一个返回非可选参数的API

let str = "teststring"
let data = Data(str.utf8)

迅速:

快5.倍

let myStringToConvert = "My String to Convert in Data"
let myData = myStringToConvert.data(using: .utf8)

字符串到数据:

var myStringToConvert = "My String to Convert in Data"
var myData = myStringToConvert.data(using: String.Encoding.utf8)

数据到字符串:

var backToMyString = String(data: myData!, encoding: String.Encoding.utf8) as String!

OBJECTIVE - C:

德国…

NSString* myStringToConvert= @"My String to Convert in Data";
NSData* myData=[str dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];

ns戴:

NSString* backToMyString = [[NSString alloc] initWithData: myData encoding:NSUTF8StringEncoding];