我需要找到一种将用户键入的任意字符转换为ASCII表示形式以发送到网络服务的方法。我当前的方法是创建一个查找字典并发送相应的代码。创建此字典后,我发现很难维护并确定它是否完整:
__asciiKeycodes[@"F1"] = @(112);
__asciiKeycodes[@"F2"] = @(113);
__asciiKeycodes[@"F3"] = @(114);
//...
__asciiKeycodes[@"a"] = @(97);
__asciiKeycodes[@"b"] = @(98);
__asciiKeycodes[@"c"] = @(99);
是否有更好的方法从用户键入的任意键(使用标准104键盘)中获取ASCII字符代码?
目标C具有基本C基本数据类型。您可以采取一些小技巧。您要将keyStroke设置为char
,然后将其强制转换为int
。c中从achar
到a的默认转换int
是char
的ascii值。这是一个简单的例子。
char character= 'a'; NSLog("a = %ld", (int)test);
console output =a = 97
相反,将int转换为char;
int asciiValue= (int)97; NSLog("97 = %c", (char)asciiValue);
控制台输出= 97 = a
另外,您可以在int或char的初始化内进行直接转换,并将其存储在变量中。
char asciiToCharOf97 = (char)97; //Stores 'a' in asciiToCharOf97
int charToAsciiOfA = (int)'a'; //Stores 97 in charToAsciiOfA
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句