iOS : How do i convert NSString to NSMutableData? - ios

I am trying to convert my NSString to NSMutableData without the use of encoding. How can I accomplish this?

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?

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.

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.

Related

Convert hexadecimal into short hexadecimal string like "Zx4E5"

I'm working on a bank application where we convert string into hexadecimal string and some time length of hexadecimal string is too long so i said to my senior developer to encode it into short string like as i mention in title and than decode it back to real string.
He said to me its not possible so i want to know is it possible or not?
And if its possible then how ?
He is using following code for encoding and decoding
NSString *plainString = #"foo";
Encoding
NSData *plainData = [plainString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [plainData base64EncodedStringWithOptions:0];
NSLog(#"%#", base64String); // Zm9v
Decoding
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(#"%#", decodedString); // foo

Convert string to mutable data - objective c [duplicate]

This question already has answers here:
objective-c: how to assign string to NSMutableData?
(4 answers)
Closed 6 years ago.
How to convert a string into mutable data in objective c
You can convert NSString to NSData by using
NSString* str = #"mystring";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *yourdata = [data mutableCopy];
hope it helps you :)
You can do something like,
NSString *str = #"any string";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *mutableData = [data mutableCopy];;
Hope this will help :)

iOS filter out characters that cannot be encoded

Hi I am developing an iOS application and I am facing the following question.
I have a NSString that contains Traditional Chinese, Simplified Chinese and probably some special characters like "®".
When I want to convert this NSString to NSData using
[string dataUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingBig5)]
the NSData returned is nil and I want to remove the invalid characters that cannot be encoded.
How can I do this? Thanks!!
NSString *string = #"®한자";
NSString *newStr = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *data = [newStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *originalString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] stringByRemovingPercentEncoding];

NSString convert string to NSData using encoding

I have got small issue in my app.
I make NSMutableURLRequest and send body to my webserice.
jsonRequest = #"{\"s\":\"deón\"}";
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
I need change "ó" symbol to \u00F3.
Any idea?
I would suggest you another way around if you want to deploy on target >=iOS5. It involves more steps, but you can be sure about the correct result, it's less error prone.
NSDictionary * jsonDict = #{ #"s" : #"deón" };
NSData * requestData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:nil];
convert in UTF-8
NSData *data = [arrayString dataUsingEncoding:NSUTF8StringEncoding];
Try this code it will near to your requirement
NSString *jsonRequest = #"{\"s\":\"deón\"}";
const char *c = [jsonRequest cStringUsingEncoding:NSISOLatin1StringEncoding];

'initWithBase64Encoding' is deprecated?

I want to get the data from the string. i am using the following code but it seems to be deprecated.
NSData *data=[[NSData alloc]initWithBase64Encoding:(NSString *)dict];
I got the data. But its give me the warning that 'initWithBase64Encoding' is deprecated.
So is there any other method that will return data?
use this
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
instand of
NSData *data=[[NSData alloc]initWithBase64Encoding:(NSString *)dict];
Returns a data object initialized with the given Base-64 encoded string. (Deprecated in iOS 7.0. You should transition to either initWithBase64EncodedString:options: or initWithBase64EncodedData:options:.)
Use following
ENCODE
NSString *myString = #"Developer";
NSData *myData = [myString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [myData base64EncodedStringWithOptions:0];
NSLog(#"%#", base64String);
DECODE:
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(#"%#", decodedString);

Resources