如何将ASCII值转换为Objective-C中的字符?

top*_*ace 36 iphone ascii

我想知道是否有人在Objective-C中为iPhone开发提供以下php功能:

  1. ord()#返回字符串第一个字符的ASCII值.
  2. chr()#返回指定ASCII值的字符.

非常感谢!

all*_*eus 117

这是你如何使用ASCII值和NSString.请注意,由于NSString使用unichars,非ASCII字符串可能会出现意外结果.

// 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
Run Code Online (Sandbox Code Playgroud)


luv*_*ere 12

//char to int ASCII-code
char c = 'a';
int ascii_code = (int)c;

//int to char
int i = 65; // A
c = (char)i;
Run Code Online (Sandbox Code Playgroud)

  • c中的char是一个"较小"的int,因此您不需要将char转换为int (2认同)