简体   繁体   English

如何在iOS中将范围从U + 0000到U + 0099的Unicode附加到NSString

[英]How to append unicode ranging U+0000 to U+0099 to NSString in iOS

How to append unicode ranging U+0000 to U+0099 To NSString in iOS. 如何在iOS中将范围从U+0000U+0099 unicode附加到NSString。 I have used the following link for reference http://en.wikipedia.org/wiki/List_of_Unicode_characters 我已使用以下链接作为参考http://en.wikipedia.org/wiki/List_of_Unicode_characters

Try to use this one.... 尝试使用此...。

NSString uses UTF-16 to store codepoints internally, so those in the range you're looking for (U+1F300 to U+1F6FF) will be stored as a surrogate pair (four bytes). NSString使用UTF-16在内部存储代码点,因此您要查找的范围(U + 1F300至U + 1F6FF)中的代码点将作为代理对 (四个字节)存储。 Despite its name, characterAtIndex: (and unichar) doesn't know about codepoints and will give you the two bytes that it sees at the index you give it (the 55357 you're seeing is the lead surrogate of the codepoint in UTF-16). 尽管其名称为characterAtIndex :(和unichar),但它不知道代码点,并将为您提供在给定索引处看到的两个字节(您看到的55357是UTF-16中代码点的主要替代品) )。

To examine the raw codepoints, you'll want to convert the string/characters into UTF-32 (which encodes them directly). 要检查原始代码点,您需要将字符串/字符转换为UTF-32(直接对它们进行编码)。 To do this, you have a few options: 为此,您有几种选择:

1) Get all UTF-16 bytes that make up the codepoint, and use either this algorithm or CFStringGetLongCharacterForSurrogatePair to convert the surrogate pairs to UTF-32. 1)获取构成代码点的所有UTF-16字节,并使用此算法CFStringGetLongCharacterForSurrogatePair将代理对转换为UTF-32。

2) Use either dataUsingEncoding: or getBytes:maxLength:usedLength:encoding:options:range:remainingRange: to convert the NSString to UTF-32, and interpret the raw bytes as a uint32_t. 2)使用dataUsingEncoding:或getBytes:maxLength:usedLength:encoding:options:range:remainingRange:将NSString转换为UTF-32,并将原始字节解释为uint32_t。

3) Use a library like ICU . 3)使用ICU之类的库。

I'm not sure this is 100% correct solution, but it works: 我不确定这是否是100%正确的解决方案,但是它可以正常工作:

NSString *uniString = [NSString stringWithFormat:@"%C", (unichar)0x0021];

Where 0x0021 is your unicode char code. 其中0x0021是您的Unicode字符代码。

You can test it with this loop: 您可以使用以下循环对其进行测试:

for (unichar ch = 0x0000; ch <= 0x0099; ch++) {
    NSString *uniString = [NSString stringWithFormat:@"%C", ch];
    NSLog(@"%@", uniString);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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