如何将NSString
值转换为NSData
?
14 回答
NSString* str = @"teststring";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSString *str = @"helowrld";
// This converts the string to an NSData object
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
你可以参考这个链接
做:
NSData *data = [yourString dataUsingEncoding:NSUTF8StringEncoding];
然后随意继续NSJSONSerialization:JSONObjectWithData
。
更正有关 NULL 终止符的答案
在评论、官方文档和验证之后,此答案已更新,以删除所谓的 NULL 终止符:
正如dataUsingEncoding:所记录的:
返回值
dataUsingEncoding:allowLossyConversion:
以 NO 作为第二个参数调用的结果正如getCString:maxLength:encoding:和cStringUsingEncoding:所记录的:
请注意,返回的数据
dataUsingEncoding:allowLossyConversion:
不是严格的 C 字符串,因为它没有 NULL 终止符
如果 Swift Developer 来到这里,
从 NSString / String 转换为 NSData
var _nsdata = _nsstring.dataUsingEncoding(NSUTF8StringEncoding)
目标-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
首先,您应该使用dataUsingEncoding:
而不是通过UTF8String
. 仅UTF8String
当您需要C
该编码中的字符串时才使用。
然后,对于UTF-16
,只需传递NSUnicodeStringEncoding
而不是NSUTF8StringEncoding
在您的dataUsingEncoding:
消息中。
对于 Swift 3,您将主要从 转换String
为Data
.
let myString = "test"
let myData = myString.data(using: .utf8)
print(myData) // Optional(Data)
NSString *str = @"hello";
NSData *data = [NSData dataWithBytes:str.UTF8String length:str.length];
目标-C:
NSString 到 NSData:
NSString* str= @"string";
NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];
NSData 到 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)
迅速:
斯威夫特 5.x
let myStringToConvert = "My String to Convert in Data"
let myData = str.data(using: .utf8)
字符串到数据:
var myStringToConvert = "My String to Convert in Data"
var myData = testString.data(using: String.Encoding.utf8)
数据到字符串:
var backToMyString = String(data: myData!, encoding: String.Encoding.utf8) as String!
目标 C:
NSString 到 NSData :
NSString* myStringToConvert= @"My String to Convert in Data";
NSData* myData=[str dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSData 到 NSString :
NSString* backToMyString = [[NSString alloc] initWithData: myData encoding:NSUTF8StringEncoding];
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)
在 Swift 中有一个返回非可选的 API
let str = "teststring"
let data = Data(str.utf8)