Search code examples
objective-cnsstring

How to get the Ascii Integer value of the first letter of a NSString in Objective-C?


In Objective-C, how to get the Ascii Integer value of the first letter of a NSString?


Solution

  • if ([aString length] > 0) {
        unichar firstCharacter = [aString characterAtIndex: 0];
        // ...
    }
    

    That's all. unichar is an alias of unsigned short and so is an integer type. However, there is no guarantee the character is part of ASCII, as NSString is Unicode-based.