简体   繁体   中英

Objective-C int convert to UTF-8

I want to convert this int num: 53392 to cyrillic letter A . I use this: NSString *string = [NSString stringWithFormat:@"%c", 53392]; But this now work properly and return me strange symbols. Can someone help me?

First, use %C format (capital letter C). Second, code for cyrillic A is 0x0410, not your number.

创建包含Unicode字符的NSString的正确方法是使用stringWithUTF8String和\\ u ####转义序列,如下所示:

NSString *aString = [NSString stringWithUTF8String:"To be continued\u2026"];

The integer you have, 53392, is the two bytes 0xD0 and 0x90 packed into an integer, which are the UTF-8 encoding of the Unicode 0x410 (decimal 1040) cyrillic capital A.

There are multiple ways to do your conversion, here are two.

If you know the UTF-8 sequence (which you can extract out of the integer if needed) you can convert it to an NSString using a sequence like:

char buf[2];
buf[0] = 0xD0;
buf[1] = 0x90;
NSString *cyrillicA = [[NSString alloc] initWithBytes:buf length:2 encoding:NSUTF8StringEncoding];

If you know the UTF-16 value you can use the %C format:

NSString *cyrillicA = [NSString stringWithFormat:@"%C", 0x410];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM