问题
For Objective-C in iOS:
If I have a string how could I read the unicode numeric value of an individual character?
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 "ҕ"
Thanks!
回答1:
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];
回答2:
NSString *c = [NSString stringWithFormat:@"%C", unicodeValue];
int unicode = [c intvalue];
// then create a new one
unicode+=100;
// create a unicode value again
NSString *uniString = [NSString stringWithCharacters:(unichar *)&unicode length:1];
来源:https://stackoverflow.com/questions/10567598/ios-unicode-numeric-value-of-individual-characters-in-a-string