字符转换仍不明确(Objective-C)

时间:2009-10-05 03:01:02

标签: objective-c character

首先,感谢所有回答我前两个问题的人。我已经尝试了所建议的但仍然无效。我以为我会用我正在使用的代码创建另一篇文章。只是为了一些背景:我正在为我的儿子创建一个猜谜游戏,我需要将字符形式字符更改为十进制到字符。我添加了代码和日志。我希望这更有帮助。

-(IBAction)CHANGE:(id)sender
NSString    *working_text;
NSInteger   working_text_length;
NSInteger   new_key_char_num;
NSInteger   key_char_num;
NSInteger   key_char_position = 0;
NSInteger   char_num = 0;
NSString    *altered_text;

working_text = UITextView_1.text; 
working_text_length = working_text.length;
altered_text = @"";
UITextView_1.text = @"";

int k = 0;
while (k < working_text_length)
{
char_num = [working_text characterAtIndex:k];           
key_char_num        = 65;
new_key_char_num    = char_num  + key_char_num;
    UITextView_1.text = [UITextView_1.text   
    stringByAppendingFormat:@"%c",new_key_char_num];   



    NSLog(@"char_num................%c",char_num);
    NSLog(@"char_num................%d",char_num);
    NSLog(@"key_char_num........... %d",key_char_num);
    NSLog(@"new_key_char_num........%d",new_key_char_num);
    NSLog(@"new_key_char_num........%c",new_key_char_num);
    NSLog(@" ");

    char_num = 0;
    k++;
}


 NSLog(@"UITextView_1.text full text...%@",UITextView_1.text);
 NSLog(@"first  %c",[UITextView_1.text characterAtIndex:0]);
 NSLog(@"second %c",[UITextView_1.text characterAtIndex:1]);
 NSLog(@"third  %c",[UITextView_1.text characterAtIndex:2]);


 // release code is not shown


}

日志

2009-10-04 22:43:04.949 DaVinci SMS[20757:20b] char_num................A
2009-10-04 22:43:04.951 DaVinci SMS[20757:20b] char_num................65
2009-10-04 22:43:04.951 DaVinci SMS[20757:20b] key_char_num........... 65
2009-10-04 22:43:04.952 DaVinci SMS[20757:20b] new_key_char_num........130
2009-10-04 22:43:04.953 DaVinci SMS[20757:20b] new_key_char_num........Ç
2009-10-04 22:43:04.953 DaVinci SMS[20757:20b]  
2009-10-04 22:43:04.954 DaVinci SMS[20757:20b] char_num................B
2009-10-04 22:43:04.955 DaVinci SMS[20757:20b] char_num................66
2009-10-04 22:43:04.956 DaVinci SMS[20757:20b] key_char_num........... 65
2009-10-04 22:43:04.956 DaVinci SMS[20757:20b] new_key_char_num........131
2009-10-04 22:43:04.957 DaVinci SMS[20757:20b] new_key_char_num........É
2009-10-04 22:43:04.957 DaVinci SMS[20757:20b]  
2009-10-04 22:43:04.958 DaVinci SMS[20757:20b] char_num................C
2009-10-04 22:43:04.959 DaVinci SMS[20757:20b] char_num................67
2009-10-04 22:43:04.959 DaVinci SMS[20757:20b] key_char_num........... 65
2009-10-04 22:43:04.960 DaVinci SMS[20757:20b] new_key_char_num........132
2009-10-04 22:43:04.960 DaVinci SMS[20757:20b] new_key_char_num........Ñ
2009-10-04 22:43:04.961 DaVinci SMS[20757:20b]  
2009-10-04 22:43:04.961 DaVinci SMS[20757:20b] UITextView_1.text...ÇÉÑ
2009-10-04 22:43:04.962 DaVinci SMS[20757:20b] first  «
2009-10-04 22:43:04.962 DaVinci SMS[20757:20b] second …
2009-10-04 22:43:04.963 DaVinci SMS[20757:20b] third  —

我在UITextView中输入了字符'ABC'。我现在只使用三个字符。如果您注意到底部日志,则完整的UITextView_1.text字符串不等于我使用characterAtIndex方法提取的单个字符。这让我发疯了。

1 个答案:

答案 0 :(得分:1)

如果你想从char转到整数并返回,不需要添加任何东西 - 你可以简单地将unichar视为整数:

NSString *s = @"ABC";
unichar c = [s characterAtIndex: 0];
NSLog(@"char code: %i, char: %c", c, c); // output: char code: 65, char: A