How to convert ASCII value to a character in Objective-C?

后端 未结 2 1084
感情败类
感情败类 2020-12-01 03:32

I was wondering if anyone has the following php function equivalents in Objective-C for iPhone development:

  1. ord() # returns the ASCII value of the first charac
相关标签:
2条回答
  • 2020-12-01 03:58
    //char to int ASCII-code
    char c = 'a';
    int ascii_code = (int)c;
    
    //int to char
    int i = 65; // A
    c = (char)i;
    
    0 讨论(0)
  • 2020-12-01 04:18

    This is how you can work with ASCII values and NSString. Note that since NSString is working with unichars, there could be unexpected results for a non ASCII string.

    // NSString to ASCII
    NSString *string = @"A";
    int asciiCode = [string characterAtIndex:0]; // 65
    
    // ASCII to NSString
    int asciiCode = 65;
    NSString *string = [NSString stringWithFormat:@"%c", asciiCode]; // A
    
    0 讨论(0)
提交回复
热议问题