I am trying to convert my NSString
to NSMutableData
without the use of encoding
. How can I accomplish this?
Asked
Active
Viewed 647 times
-3

Aleksander Azizi
- 9,829
- 9
- 59
- 87

vinay
- 1,276
- 3
- 20
- 34
-
2how do you mean _without encoding_? what do you want to achieve here? – holex Jun 12 '14 at 15:43
-
An `NSString` internally used UTF16 but I doubt you want those bytes. Thus what encoding do you want? What if the string contains an Å or even a ? – zaph Jun 12 '14 at 15:52
-
Possible duplicate of [Convert NSString to \`NSMutableData\` in Objective C?](http://stackoverflow.com/questions/37023987/convert-nsstring-to-nsmutabledata-in-objective-c) – Avt May 04 '16 at 11:20
3 Answers
1
How about this?
NSString *str = @"grazz";
NSMutableData *data1 = [[str dataUsingEncoding:NSUTF8StringEncoding] mutableCopy];
NSMutableData *data2 = [NSMutableData dataWithBytes:[str UTF8String] length:[str length]];
ps: I'm not sure about "without encoding", what did you mean by that?

3329
- 1,411
- 13
- 17
-
respecting your efforts sir. i think your answer will helps later. – Soorej Babu May 04 '16 at 10:45
1
As pointed out here: NSString value to NSData, you can use dataUsingEncoding
to convert an NSString
to NSData
.
To place it in a NSMutableData
container, simply make a mutableCopy
, like so:
NSString *string = @"MyString";
NSMutableData *mutableData = [[string dataUsingEncoding:NSUTF8StringEncoding] mutableCopy];
As for doing this "without the use of encoding
", the string will always be encoded. Even if you don't set any specific encoding
the string will still be encoded. I think the solution above should solve your problem.

Community
- 1
- 1

Aleksander Azizi
- 9,829
- 9
- 59
- 87
0
why the question is downvoted?
NSString* str = @"teststring";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *yourdata = [data mutableCopy];
first you have to convert to NSData
and then to NSMutableData
. From my knowledge, we cannot convert directly to NSMutableData
from NSString
.

Soorej Babu
- 350
- 2
- 19