22

I am trying to parse a feed from a json webservice on my iPhone however the utf8 conversion is not working the way it should or am I doing something wrong Here is apart of the feed
est un r\u00c3\u00aave en noir

Here is the code I have written to convert the above:

NSString* str1 = @"est un r \u00c3\u00aa ve en noir";

NSString* str = [NSString stringWithUTF8String:[str1 cStringUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"Converted UTF   %@",str);

This is the output that I am getting:
Converted UTF est un r ê ve en noir

The expected output should be
Converted UTF est un rêve en noir

When I checked the UTF table and used a utf converter online the output is correctly got with the same code and using the string @"est un r\u00EAve en noir" but I think that the UTF16 Representation if I am not mistaken. Now am really in a fix how to parse this feed.

Gaurav Ghate
  • 783
  • 2
  • 7
  • 7
  • perfect solution http://stackoverflow.com/questions/6204718/status-code-0-on-google-geocode-api-from-iphone-sim-but-works-fine-on-web-non/6204970#6204970 congratulations... – Moacir Apr 14 '15 at 13:23

4 Answers4

40

After much grief I have figured this out. Use this.

NSString *correctString = [NSString stringWithCString:[utf8String cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];
  • No problem. Just a warning, I've been having memory leak issues with this solution. I *think* i fixed with [NSString stringWithString:(the above code)] – яοвοτағτєяаււ Jan 30 '12 at 19:18
  • 1
    You are a life saver. After searching for so many hours, I was almost about to give up. And then, I found this sweet little piece of code. Thanks a lot. This should be the accepted answer. – Prince iPhone Nov 17 '13 at 19:18
  • If that solution works for you then your JSON parser is working incorrectly and interpreting incoming data as Latin 1 rather than as UTF-8. Depending on how you have it wired up, you should also check what you're getting in your HTTP headers (though `NSJSONSerialization` doesn't take them, but then again it gets the right answer in the first place) – Tommy Nov 18 '13 at 23:17
  • You saved my day. And night! Thanks a lot! – eilas Oct 20 '15 at 00:33
  • Thanks a lot! Perfect piece of code! Saved me hours! – user3079872 Oct 22 '15 at 17:46
11

I think the test case is broken; the following:

NSString* str1 = @"est un r \u00c3\u00aa ve en noir";
NSLog(@"%@", str1);

Also outputs 'est un r ê ve en noir'. However, this:

NSString* str1 = @"est un rêve en noir";
NSLog(@"%@", str1);

Outputs 'est un rêve en noir', as does:

NSString* str1 = @"est un rêve en noir";

NSString* str = [NSString stringWithUTF8String:[str1 cStringUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"%@", str);

And ditto for the slightly shorter version:

NSString* str1 = @"est un rêve en noir";

NSString* str = [NSString stringWithUTF8String:[str1 UTF8Encoding]];
NSLog(@"%@", str);

And, indeed:

char *str1 = "est un r\xc3\xaave en noir";

NSString* str = [NSString stringWithUTF8String:str1];
NSLog(@"%@", str);

I think it's a question of JSON, not UTF8. The \u followed by four hexadecimal digits is JSON's way of encoding a generic UTF16 character, it's not an inherent part of UTF. So NSString doesn't know how to deal with it. Your JSON parser needs to be adapted to parse escape sequences properly.

Tommy
  • 99,986
  • 12
  • 185
  • 204
0

In iOS 9 don't need do anything. It is converted automatically.

   NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
                                                               fromData:postData completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

                                                                   NSLog(@"response header: %@",response);

                                                                   NSString* aStr;
                                                               aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                                               NSLog(@"response data: %@", aStr);

                                                               NSDictionary * json  = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

                                                               for (NSDictionary *dict in json)
                                                               {
                                                                   NSLog(@"%@", dict);
                                                                   NSString *name = [dict objectForKey:@"name"];
                                                                   NSString *msg = [dict objectForKey:@"message"];
                                                                   NSString *topic = [dict objectForKey:@"topic"];
                                                                   NSLog(@"*********** %@, %@, %@", name,topic,msg);

                                                                   // now do something
                                                                   }
dip
  • 129
  • 1
  • 4
0

While sending to server

NSString* str1 = @"est un r \u00c3\u00aa ve en noir";
    NSData *str1Data = [str1 dataUsingEncoding:NSNonLossyASCIIStringEncoding];
    NSString  *str2 = [[NSString alloc] initWithData:str1Data encoding:NSUTF8StringEncoding];

After receiving you can decode like this

NSData *recivedStrData = [recivedStr dataUsingEncoding:NSUTF8StringEncoding];
    NSString *strResult = [[NSString alloc] initWithData:recivedStrData encoding:NSNonLossyASCIIStringEncoding];

This works for me!

Sandeep Khade
  • 2,832
  • 3
  • 21
  • 37