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 :)
Related
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
This question already has answers here:
How to parse JSON in Objective-C
(2 answers)
Closed 6 years ago.
I have JSON DATA
{"respcode":0,"policyid":"1731958","Insuredid":"5625869"}
How can I get respcode, policyid and Insuredid in individual variable using parsing.
Try out the below code:
NSString* path = [[NSBundle mainBundle] pathForResource:#"JSON" ofType:#"json"];
NSString* jsonString = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *responseDictionary = [NSJSONSerialization
JSONObjectWithData:jsonData
options:0
error:&error];
if(! error) {
NSString *insuredId = [responseDictionary objectForKey:#"Insuredid"];
NSString *policyid = [responseDictionary objectForKey:#"policyid"];
NSString *respcode = [responseDictionary objectForKey:#"respcode"];
NSLog(#"insuredId : %# & policyid : %# & respcode : %#",insuredId,policyid,respcode);
} else {
NSLog(#"Error in parsing JSON");
}
//JSON.json
{"respcode":0,"policyid":"1731958","Insuredid":"5625869"}
Edited If your JSON DATA is already a Dictionary, you can access them using:
NSLog(#"%#",jsonData[#"respcode"]); // 0
NSLog(#"%#",jsonData[#"policyid"]); // 1731958
NSLog(#"%#",jsonData[#"Insuredid"]); // 5625869
I hope this can help you. If not, let me know more information about your code, so I can try a better answer.
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];
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];
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.