【发布时间】:2020-02-13 14:03:35
【问题描述】:
我的主要代码需要比较一个整数 ASCII 命令,我想知道是否有解决方案来优化这个。
我可以在串行总线上从 '1' 发送到 '128' 并到达正确的对象,我是这样做的:
// this is how I deal with the data sent on the serial bus
int translateASCII(char requestBuffer[10]){
char word[4] = {0};
word[0] = (int)requestBuffer[0];
word[1] = (int)requestBuffer[1];
word[2] = (int)requestBuffer[2];
int n = atoi(word);
return n;
}
void interpreteASCII(int ascii){
if (ascii > 0 && ascii < 33){
if (ascii < 9){
blabla
}
else if (ascii < 17){
blabla
}
else if (ascii < 25){
blabla
}
else
blabla
}
else if (ascii < 65){
if (ascii < 41){
blabla
}
else if { ... }
}
}
所以我正在寻找一种优化这种“架构”的方法。还考虑了一个 switch case 函数,但它似乎只将我的 ASCII 变量与单个整数进行比较,例如:
switch (ascii){
case '8':
blabla
这不是我要找的,因为指令实际上取决于按 32 间隔排序的 ASCII 变量,而这些变量本身必须按 8 个值的间隔排序。
【问题讨论】:
-
或者,如果您应该明确处理 ASCII,但不确定您的程序语言环境字符集是否与 ASCII 兼容,那么请创建您自己的字符分类函数。它将使您的代码更具可读性。
-
事实是我对 C 不是超级友好,像初学者或其他什么,所以我真的不知道如何正确地做到这一点
-
那些神奇的数字 17、25、33、41 和 65 是什么?
-
我用这些来制作 8 的范围
标签: c# optimization range comparison