Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 598 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I convert an NSString value to NSData?

#11
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](

[To see links please register here]

), this answer was updated regarding the removal of an alleged NULL terminator:

1. As documented by [dataUsingEncoding:](

[To see links please register here]

):
>###Return Value
The result of invoking `dataUsingEncoding:allowLossyConversion:` with NO as the second argument

2. As documented by [getCString:maxLength:encoding:](

[To see links please register here]

) and [cStringUsingEncoding:](

[To see links please register here]

):
>note that the data returned by `dataUsingEncoding:allowLossyConversion:` is not a strict C-string since it **does not have a NULL terminator**
Reply

#12
**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!
Reply

#13
Update Swift 5.x

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

#14
In Swift there is an API which returns a **non-optional**

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

#15
<b>Swift:</b>

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

<b>OBJECTIVE C:</b>

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];
```


Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through