问题描述
从 NSString
获取Unicode值的最简单方法是什么?例如,
What's the easiest way to get the Unicode value from an NSString
? For example,
NSString *str = "A";
NSString *hex;
现在,我要设置 hex
到 str
(ie 0041)的Unicode值...我将如何做呢?
Now, I want to set the value of hex
to the Unicode value of str
(i.e. 0041)... How would I go about doing that?
推荐答案
unichar
类型定义为16位unicode值(例如,间接记录在的说明),您可以获得 unichar
从 NSString
中的给定位置使用,或使用如果你想从 NSString填充一个C数组的unichar
The
unichar
type is defined to be a 16-bit unicode value (eg, as indirectly documented in the description of the %C specifier), and you can get a unichar
from a given position in an NSString
using characterAtIndex:
, or use getCharacters:range:
if you want to fill a C array of unichars from the NSString
more quickly than by querying them one by one.
NSUTF32StringEncoding
也是一个有效的字符串编码,以及一些特定于端序的变体,以防万一你想要绝对的未来证明。你会得到一个C数组,这些数组使用了更加长远的。
NSUTF32StringEncoding
is also a valid string encoding, as are a couple of endian-specific variants, in case you want to be absolutely future proof. You'd get a C array of those using the much more longwinded getBytes:maxLength:usedLength:encoding:options:range:remainingRange:
.
编辑:例如
NSString *str = @"A";
NSLog(@"16-bit unicode values are:");
for(int index = 0; index < [str length]; index++)
NSLog(@"%04x", [str characterAtIndex:index]);
这篇关于获取NSString的Unicode点并将其放入另一个NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!