How do I convert an NSString
value to NSData
?
NSString* str = @"teststring";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSString
uses UTF-16 internally so there might be a slight performance gain because it does not have to do a UTF-16 <-> UTF-8 conversion. Personally, we prefer (as @Meitner suggests) robustness over performance and use UTF-8 everywhere. –
Vertebra NSString *str = @"helowrld";
// This converts the string to an NSData object
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
you can take reference from this link
NSData *data = [@"helowrld" dataUsingEncoding:NSUTF8StringEncoding];
–
Make Do:
NSData *data = [yourString dataUsingEncoding:NSUTF8StringEncoding];
then feel free to proceed with NSJSONSerialization:JSONObjectWithData
.
Correction to the answer regarding the NULL terminator
Following the comments, official documentation, and verifications, this answer was updated regarding the removal of an alleged NULL terminator:
As documented by dataUsingEncoding::
Return Value
The result of invoking
dataUsingEncoding:allowLossyConversion:
with NO as the second argumentAs documented by getCString:maxLength:encoding: and cStringUsingEncoding::
note that the data returned by
dataUsingEncoding:allowLossyConversion:
is not a strict C-string since it does not have a NULL terminator
dataUsingEncoding:
does not return null-terminated data. Only UTF8String
and other methods that return a C string return a null-terminated string. –
Lakeishalakeland dataUsingEncoding:
, by inspecting the data's length and bytes. (Note that some encodings, such as UTF-16, will emit 0x00 bytes! But those aren't terminators; they're part of larger code units. And it is possible to have a U+0000, which will be encoded as such, but is not a terminator.) –
Lakeishalakeland const unichar buffer[1] = { 0x0000 }; NSString *string = [NSString stringWithCharacters:buffer length:1]; NSData *data = [string dataUsingEncoding:NSUTF16BigEndianStringEncoding]; NSLog(@"%lu %@", (unsigned long int)data.length, data);
–
Lakeishalakeland (note that the data returned by dataUsingEncoding:allowLossyConversion: is not a strict C-string since it does not have a NULL terminator)
. I must have missed this earlier. I'll be sure to write up anything in the future, though. –
Bewick cStringUsingEncoding:
. I was looking under dataUsingEncoding:
.) –
Lakeishalakeland In case of Swift Developer coming here,
to convert from NSString / String to NSData
var _nsdata = _nsstring.dataUsingEncoding(NSUTF8StringEncoding)
Objective-C:
NSString *str = @"test string";
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:str];
NSString *thatStr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
Swift:
let str = "test string"
let data = NSKeyedArchiver.archivedData(withRootObject: str)
let thatStr = NSKeyedUnarchiver.unarchiveObject(with: data) as! String
First off, you should use dataUsingEncoding:
instead of going through UTF8String
. You only use UTF8String
when you need a C
string in that encoding.
Then, for UTF-16
, just pass NSUnicodeStringEncoding
instead of NSUTF8StringEncoding
in your dataUsingEncoding:
message.
For Swift 3, you will mostly be converting from String
to Data
.
let myString = "test"
let myData = myString.data(using: .utf8)
print(myData) // Optional(Data)
Objective-C:
NSString to NSData:
NSString* str= @"string";
NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];
NSData to NSString:
NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
Swift:
String to Data:
var testString = "string"
var somedata = testString.data(using: String.Encoding.utf8)
Data to String:
var backToString = String(data: somedata!, encoding: String.Encoding.utf8) as String!
NSString *str = @"hello";
NSData *data = [NSData dataWithBytes:str.UTF8String length:str.length];
str
contains code points larger than 127. This is because str.length
gives the number of Unicode characters, not the number of bytes. For example, if str
is @"にほんご"
, str.length
gives 4 while str.UTF8String
actually contains 12 bytes. Even if you replace str.length
by strlen(str.UTF8String)
, it will still be wrong for the case where str
contains the NULL character, such as @"にほ\0んご"
. –
Discobolus Update Swift 5.x
let str = "teststring"
let data = str.data(using: .utf8)
Swift:
Swift 5.x
let myStringToConvert = "My String to Convert in Data"
let myData = myStringToConvert.data(using: .utf8)
String to Data:
var myStringToConvert = "My String to Convert in Data"
var myData = myStringToConvert.data(using: String.Encoding.utf8)
Data to String:
var backToMyString = String(data: myData!, encoding: String.Encoding.utf8) as String!
OBJECTIVE C:
NSString to NSData :
NSString* myStringToConvert= @"My String to Convert in Data";
NSData* myData=[str dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSData to 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];
Swift
let str = "Hello World"
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: false)
In Swift there is an API which returns a non-optional
let str = "teststring"
let data = Data(str.utf8)
© 2022 - 2024 — McMap. All rights reserved.