Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何将缓冲区中的数字 char 转换为 int。
我用:
char *buffer = somebuffer; int number; number = int(buffer[i]); // pointer to a number stored as a char
但是,这会返回十进制代码(例如 if buffer[i] = '2', number = 50)。
buffer[i] = '2'
number = 50
如何将此字符数解释为 int?
您可以从字符代码中减去您的字符代码0(数字的字符代码是连续的,从 0 (ascii 48) - 9 (ascii 57) 运行)。请务必先使用isdigit检查字符是否超出范围'0'-'9'
0
isdigit
'0'
'9'
int num = 0; if (isdigit(buffer[i]) { num = buffer[i] - '0'; }