获取NSString的Unicode点并将其放入另一个NSString [英] Get Unicode point of NSString and put that into another NSString

查看:168
本文介绍了获取NSString的Unicode点并将其放入另一个NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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值(例如,间接记录在%C说明符的说明),您可以获得 unichar NSString 中的给定位置使用 characterAtIndex: ,或使用 getCharacters:range: 如果你想从 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数组,这些数组使用了更加长远的 getBytes:maxLength:usedLength:encoding:options:range:remainingRange: code>

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆