2

After hours of research I gave up. I receive text data from a WebService. For some case, the text is inJapanese, and the WS returns its Unicoded version. For example: \U00e3\U0082\U008f I know that this is a Japanese char.

I am trying to display this Unicode char or string inside a UILabel. Since the simple setText method does'nt display the correct chars, I used this (copied) routine:

unichar unicodeValue = (unichar) strtol([[[p innerData] valueForKey:@"title"] UTF8String], NULL, 16);
char buffer[2];
int len = 1;
if (unicodeValue > 127) {
    buffer[0] = (unicodeValue >> 8) & (1 << 8) - 1;
    buffer[1] = unicodeValue & (1 << 8) - 1;
    len = 2;
} else {
    buffer[0] = unicodeValue;
}
    [[cell title] setText:[[NSString alloc] initWithBytes:buffer length:len encoding:NSUTF8StringEncoding] ];

But no success: the UILabel is empty. I know that one way could be convert the chars to hex and then from hex to String...is there a simpler way?

SOLVED

First you must be sure that your server is sending UTF8 and not UNICODE CODE POINTS. The only way I found is to json_encode strings which contain UNICODE chars. Then, in iOS user unescaping following this link Using Objective C/Cocoa to unescape unicode characters, ie \u1234

Community
  • 1
  • 1
gdm
  • 7,647
  • 3
  • 41
  • 71
  • The format `\U00e3\U0082\U008f` does not make sense to me, because if those represent raw UTF-8 bytes, the most significant byte is always `00`. Anyway, You need some way to convert `\U00e3\U0082\U008f` to the bytes `0xe3 0x82 0x8f`, and then decode those bytes as UTF-8. – Esailija Dec 20 '12 at 18:46
  • The format is the output from a SBJSON parser, which in turn receives data from a WS written in php. The php code fetches data from a PostGreSql db, where some fields contain Japanese chars. For instance, I can see correctly those fields in phppgadmin.... – gdm Dec 20 '12 at 19:40

0 Answers0