<分区>
无法 #include <stdlib.h>
, 现在我需要自己写 int my_atoi(char* str)
在我的内核模块中运行。但我认为必须有更简单的方法来使用此函数和 stdlib 中的其他一些函数(atof、itoa 等)。你知道吗?
<分区>
无法 #include <stdlib.h>
, 现在我需要自己写 int my_atoi(char* str)
在我的内核模块中运行。但我认为必须有更简单的方法来使用此函数和 stdlib 中的其他一些函数(atof、itoa 等)。你知道吗?
最佳答案
您可以通过以下方式将 char*
转换为 int
而无需 atoi
,
#include "ctype.h"
int charToInt(const char *s)
{
int n;
unsigned char sign = 0;
while (isspace(*s))
{
s++;
}
if (*s == '-')
{
sign = 1;
s++;
}
else if (*s == '+')
{
s++;
}
n=0;
while (isdigit(*s))
{
n = n * 10 + *s++ - '0';
}
return sign ? -n : n;
}
int main (){
const char * g = "123456";
printf("num:%d",charToInt(g));
return 0;
}
产生输出,
123456
关于c - 如何在内核模块中使用atoi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54627126/