lundi 2 mars 2015

NSData containing ä, ö, ü, ß not returning correct characters

I have data received from server in JSON format. It is translated to NSDictionary from which I am taking string value. Problem is when I try to print that name I am expecting "ö", but getting "ö". In UTF-8, "ö" is represented as 0xC3B6. In UTF-16, "Ã" is 0xC3 and "¶" is 0xB6. What am I doing wrong so dictionary value is represented in UTF-16 instead of UTF-8? Here is code that I am using.



NSString *inptu = [[NSString alloc] initWithBytes:buffer length:len encoding:NSUTF8StringEncoding];
NSLog(@"input: %@",inptu);

NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding];
//get json from data
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSString *value = [json valueForKey:"name"];
NSLog(@"output: %@",value);


For first log I get string like this: "input: \u00C3\u00B6" For second: "output: ö"


This issue can be repeated with all other extended ASCII characters.




Aucun commentaire:

Enregistrer un commentaire