简体   繁体   English

将Unichar转换为Int问题

[英]Converting Unichar to Int Problem

I've got a strange problem trying to convert from unichar to int: 我试图从unichar转换为int有一个奇怪的问题:

I have a String containing a few numbers, like @"12345" . 我有一个包含几个数字的字符串,比如@"12345" This numbers I want to save individually, meaning I want 5 numbers, 1, 2, 3, ... . 这个数字我想单独​​保存,这意味着我想要5个数字,1,2,3,.... Now, while 现在,同时

NSLog(@"Value: %C", [myString characterAtIndex:0]);

returns 回报

Value: 1

This: 这个:

NSLog(@"Value: %@", [NSNumber numberWithUnsignedChar:[myString characterAtIndex:0]]);

returns 回报

Value: 49

I would really appreciate some help, Fabian Fabian,我真的很感激一些帮助

In the code above, you're getting exactly what you're asking for; 在上面的代码中,你得到的正是你所要求的; the numeric value of the character '1' is 49, and you're creating an NSNumber from that. 字符'1'的数值是49,并且您正在创建一个NSNumber。

If what you want is 1 , then you can can take advantage of the fact that the digits 0-9 are laid out in order in the ASCII/UTF-8 tables and subtract an appropriate value from the unichar you receive. 如果你想要的是1 ,那么你可以利用数字0-9按顺序排列在ASCII / UTF-8表格中的事实,并从你收到的unichar中减去一个合适的值。

Try out this code snippet to get you started in the right direction: 试试这段代码片段,让您开始朝着正确的方向前进:

NSString *s = @"0123456789";
for (int i = 0; i < [s length]; i++) {
    NSLog(@"Value: %d", [s characterAtIndex:i]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM