c - 如何在内核模块中使用atoi

标签 c linux-kernel kernel-module

<分区>

无法 #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/

相关文章:

c - gcc9+ 模数优化背后的数学

c - Linux内核中的_THIS_IP_宏

c - Linux内核中wake_up_sync/wake_up_interruptible_sync的用途

linux - Linux 内核代码中的 EXPORT_SYMBOL_GPL 是什么?

linux - 在设备驱动程序中访问 Linux 硬件的引脚

C + OpenGL 四元数

在 C 中将未知长度缓冲区复制到固定大小缓冲区

使用 Linux netfilter 将源 IP 地址从 struct iphdr* 转换为等效字符串

c - 如何在 C 中使用 scanf 扫描一个整数值后跟一个字符

c - 基于 Ubuntu 的发行版缺少 modules.usbmap 和 modules.pcimap