如何将NSString值转换为NSData?

时间:2009-05-23 11:20:38

标签: objective-c nsstring nsdata

如何将NSString值转换为NSData

14 个答案:

答案 0 :(得分:1400)

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

答案 1 :(得分:65)

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

您可以参考此link

答案 2 :(得分:32)

执行:

NSData *data = [yourString dataUsingEncoding:NSUTF8StringEncoding];

然后随时可以继续NSJSONSerialization:JSONObjectWithData


更正有关NULL终止符的答案

根据评论,官方文档和verifications,关于删除声称的NULL终止符,此答案已更新:

  1. 正如dataUsingEncoding:所述:

      

    返回值

         

    使用NO作为第二个参数调用dataUsingEncoding:allowLossyConversion:的结果

  2. 正如getCString:maxLength:encoding:cStringUsingEncoding:所述:

      

    请注意dataUsingEncoding:allowLossyConversion:返回的数据不是严格的C字符串,因为没有NULL终结符

答案 3 :(得分:20)

如果Swift Developer来到这里,

从NSString / String转换为NSData

var _nsdata = _nsstring.dataUsingEncoding(NSUTF8StringEncoding)

答案 4 :(得分:17)

<强>目标-C:

override func viewWillAppear(animated: Bool) {

UIApplication.sharedApplication().statusBarStyle = .Default
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarPosition: .Any, barMetrics: .Default)
self.navigationController?.navigationBar.shadowImage = UIImage()       
self.navigationController?.navigationBar.backgroundColor = UIColor.whiteColor()
self.navigationController?.navigationBar.tintColor = UIColor.blackColor()
self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
self.navigationController?.navigationBar.translucent = true

}

<强>夫特:

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

答案 5 :(得分:10)

首先,您应该使用dataUsingEncoding:而不是通过UTF8String。当您需要该编码中的UTF8String字符串时,您只能使用C

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

答案 6 :(得分:6)

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

答案 7 :(得分:6)

对于Swift 3,您将主要从String转换为Data

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

答案 8 :(得分:3)

更新 Swift 5.x

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

答案 9 :(得分:2)

Objective-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!

答案 10 :(得分:1)

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

答案 11 :(得分:1)

斯威夫特:

斯威夫特 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];

答案 12 :(得分:0)

目标-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)

答案 13 :(得分:0)

在 Swift 中有一个 API,它返回一个非可选

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