iOS - Unicode numeric value of individual characters in a string

我们两清 提交于 2019-12-25 04:05:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!