iOS - 字符串中单个字符的Unicode数字值 [英] iOS - Unicode numeric value of individual characters in a string
问题描述
对于iOS中的Objective-C:
For Objective-C in iOS:
如果我有一个字符串,如何读取单个字符的unicode数值?
If I have a string how could I read the unicode numeric value of an individual character?
例如,如果我的字符串是:Δ那个unicode字符是U + 0394,那么如何读取字符串,找出0394,然后创建一个新的字符串,那么新的字符串将是字符U + 0494或ҕ
For example if my string was: "∆" That unicode character is U+0394, so how could I read the string, figure out the "0394" then create a new string with say 100 added to that value, so the new string would be the character U+0494 or "ҕ"
谢谢!
推荐答案
首先,你的逻辑有谬误。 Δ+ 100!=ҕ
。 Unicode以十六进制计算,因此Δ实际上等于小数中的 916
,而不是 394
。因此, 0x0494 - 0x0394 = 0x0100
,等于 256
。
First, there is a fallacy in your logic. ∆ + 100 != ҕ
. Unicode is evaluated in base-16 (hex), so '∆' is actually equal to 916
in decimal, not 394
. Thus, 0x0494 - 0x0394 = 0x0100
, which equals 256
.
考虑到这一点,你的代码应该看起来像这样:
With that in mind, your code should look something like this:
unichar delta = 0x0394;
unichar weirdB = delta + 0x0100;
NSString *deltaStr = [NSString stringWithCharacters:&delta length:1];
NSString *weirdBString = [NSString stringWithCharacters:&weirdB length:1];
这篇关于iOS - 字符串中单个字符的Unicode数字值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!