Eimantas
Eimantas

Reputation: 49354

Composing unicode char format for NSString

I have a list of unicode char "codes" that I'd like to print using \u escape sequence (e.g. \ue415), as soon as I try to compose it with something like this:

// charCode comes as NSString object from PList
NSString *str = [NSString stringWithFormat:@"\u%@", charCode];

the compiler warns me about incomplete character code. Can anyone help me with this trivial task?

Upvotes: 5

Views: 7611

Answers (3)

Nianliang
Nianliang

Reputation: 3066

Based on codes from @Vladimir, this works for me:

NSUInteger codeValue;
[[NSScanner scannerWithString:@"0xf8ff"] scanHexInt:&codeValue];
NSLog(@"%C", (unichar)codeValue);

not leading by "\u" or "\\u", from API doc:

The hexadecimal integer representation may optionally be preceded
by 0x or 0X. Skips past excess digits in the case of overflow,
so the receiver’s position is past the entire hexadecimal representation.

Upvotes: 0

Vladimir
Vladimir

Reputation: 170839

I think you can't do that the way you're trying - \uxxx escape sequence is used to indicate that a constant is a unicode character - and that conversion is processed at compile-time.

What you need is to convert your charCode to an integer number and use that value as format parameter:

unichar codeValue = (unichar) strtol([charCode UTF8String], NULL, 16);
NSString *str = [NSString stringWithFormat:@"%C", charCode];
NSLog(@"Character with code \\u%@ is %C", charCode, codeValue);

Sorry, that nust not be the best way to get int value from HEX representation, but that's the 1st that came to mind

Edit: It appears that NSScanner class can scan NSString for number in hex representation:

unichar codeValue;
[[NSScanner scannerWithString:charCode] scanHexInt:&codeValue];
...

Upvotes: 14

Mark
Mark

Reputation: 6128

Beware that not all characters can be encoded in UTF-8. I had a bug yesterday where some Korean characters were failing to be encoded in UTF-8 properly.

My solution was to change the format string from %s to %@ and avoid the re-encoding issue, although this may not work for you.

Upvotes: 1

Related Questions