可能的复制品: 如何将一个字符转换为整型 字符到整数
有人能告诉我如何将 char转换成 int吗?
char
int
char c[]={'1',':','3'}; int i=int(c[0]); printf("%d",i);
当我尝试这个,它给49。
The standard function atoi() will likely do what you want.
atoi()
A simple example using "atoi":
#include <stdlib.h> #include <unistd.h> int main(int argc, char *argv[]) { int useconds = atoi(argv[1]); usleep(useconds); }
In the old days, when we could assume that most computers used ASCII, we would just do
int i = c[0] - '0';
But in these days of Unicode, it's not a good idea. It was never a good idea if your code had to run on a non-ASCII computer.
Edit: Although it looks hackish, evidently it is guaranteed by the standard to work. Thanks @Earwicker.