Determining ascii character in NSString

827 views Asked by At

In one of my UIView classes, I have the UIKeyInput protocol attached to gather input from a UIKeyboard. I'm trying to figure out what ascii character is being used when the space button is pushed (it's not simply ' ' it's something else it appears). Does anyone know what this asci character is or how I can figure out what ascii code is being used?

2

There are 2 answers

0
rmaddy On BEST ANSWER

To look at the value for each character you can do something like this:

NSString *text = ... // the text to examine
for (NSUInteger c = 0; c < text.length; c++) {
    unichar char = [text characterAtIndex:c];
    NSLog(@"char = %x", (int)char); // Log the hex value of the Unicode character
}

Please note that this code doesn't properly handle any Unicode characters in the range \U10000 and up. This includes many (all?) of the Emoji characters.

0
uchuugaka On

If you really need to know what character (or code point) it actually is, use the CFMutableString function CFStringTransform() That enables you to use transformation argument kCFStringTransformToUnicodeName to get the human readable Unicode name for example or Hex-Any to get escaped Unicode code point.

Otherwise you can do the the unichar approach to simple get the code point.