943

如何将NSString值转换为NSData

4

14 回答 14

1439
NSString* str = @"teststring";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
于 2009-05-23T11:34:10.180 回答
66
NSString *str = @"helowrld";
// This converts the string to an NSData object
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

你可以参考这个链接

于 2015-03-10T12:39:11.420 回答
33

做:

NSData *data = [yourString dataUsingEncoding:NSUTF8StringEncoding];

然后随意继续NSJSONSerialization:JSONObjectWithData


更正有关 NULL 终止符的答案

在评论、官方文档和验证之后,此答案已更新,以删除所谓的 NULL 终止符:

  1. 正如dataUsingEncoding:所记录的:

    返回值

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

  2. 正如getCString:maxLength:encoding:cStringUsingEncoding:所记录的:

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

于 2011-10-25T19:47:04.267 回答
20

如果 Swift Developer 来到这里,

从 NSString / String 转换为 NSData

var _nsdata = _nsstring.dataUsingEncoding(NSUTF8StringEncoding)
于 2015-03-03T15:28:31.137 回答
18

目标-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
于 2016-03-26T23:03:52.877 回答
10

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

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

于 2012-09-13T08:02:28.580 回答
7

对于 Swift 3,您将主要从 转换StringData.

let myString = "test"
let myData = myString.data(using: .utf8)
print(myData) // Optional(Data)
于 2016-11-01T07:59:14.480 回答
6
NSString *str = @"hello";
NSData *data = [NSData dataWithBytes:str.UTF8String length:str.length];
于 2015-11-27T17:46:07.043 回答
5

目标-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!
于 2019-03-14T08:30:30.057 回答
5

更新 Swift 5.x

let str = "teststring"
let data = str.data(using: .utf8)
于 2021-06-29T04:22:54.653 回答
3

迅速:

斯威夫特 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];
于 2021-07-05T10:43:10.793 回答
1
NSString *str = @"Banana";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:true];
于 2018-04-13T21:37:12.530 回答
0

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)
于 2018-04-17T06:19:36.737 回答
0

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

let str = "teststring"
let data = Data(str.utf8)
于 2021-06-29T04:31:33.357 回答