Unmerciful
Unmerciful

Reputation: 1335

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?

Upvotes: 0

Views: 109

Answers (3)

Ramesh
Ramesh

Reputation: 102

Try this code it will near to your requirement

 NSString *jsonRequest = @"{\"s\":\"deón\"}";

 const char *c = [jsonRequest cStringUsingEncoding:NSISOLatin1StringEncoding];

Upvotes: 0

Andrea
Andrea

Reputation: 26383

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

Upvotes: 1

Vishal Nandoriya
Vishal Nandoriya

Reputation: 75

convert in UTF-8

    NSData *data = [arrayString dataUsingEncoding:NSUTF8StringEncoding];

Upvotes: 0

Related Questions