Error[8]: Undefined offset: 33, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

linux中字符串转换函数 simple_strtoul

Linux内核中提供的一些字符串转换函数:

lib/vsprintf.c

  1. 1. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
  2. 2. unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
  3. 3. long simple_strtol(const char *cp, char **endp, unsigned int base)
  4. 4. long long simple_strtoll(const char *cp, char **endp, unsigned int base)
  5. 5. int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
  6. 6. int strict_strtol(const char *cp, unsigned int base, long *res)
  7. 7. int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
  8. 8. int strict_strtoll(const char *cp, unsigned int base, long long *res)
  9. 9. int sprintf(char *buf, const char *fmt, ...)
  10. 10. int snprintf(char *buf, size_t size, const char *fmt, ...)
  11. 11. int sscanf(const char *buf, const char *fmt, ...)

unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
功能:将一个字符串转换成unsigend long long型数据。



返回:返回转换后数据。


参数:cp指向字符串的开始,endp指向分析的字符串末尾的位置,base为要用的基数(进制数),base为0表示通过cp来自动判断基数,函数自动可识别的基数:‘0x’表示16进制,‘0’表示8进制,其它都认定为10进制。


函数可转换成数字的有效字符为:[0,f]。


举例:cp = “0x12str”,base = 0,则返回unsigned long long为18,*endp = “str”。


参数下同。


  1. static ssize_t led_brightness_store(struct device *dev,
  2. struct device_attribute *attr, const char *buf, size_t size)
  3. {
  4. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  5. ssize_t ret = -EINVAL;
  6. char *after;
  7. unsigned long state = simple_strtoul(buf, &after, 10);
  8. size_t count = after - buf;
  9. if (isspace(*after))
  10. count++;
  11. if (count == size) {
  12. ret = count;
  13. if (state == LED_OFF)
  14. led_trigger_remove(led_cdev);
  15. led_set_brightness(led_cdev, state);
  16. }
  17. return ret;
  18. }

unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
功能:将一个字符串转换成unsigend long型数据。



返回:返回转换后数据。


int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
功能:将一个字符串转换成unsigend long型。



返回:转换成功返回0,否则返回负。


res指向转换后的unsigned long数据。


说明:该函数对cp指向的字符串严格要求,cp指向的字符串必须为真正的unsigned long形式的字符串。


字符串必须以“0x”、“0”、[0,f]开始,中间全部为有效的字符[0,f],否则返回为负。


它会处理字符串最后的“\n”字符。


下同

long long simple_strtoll(const char *cp, char **endp, unsigned int base)
功能:将一个字符串转换成sigend long long型。



返回:返回转换后数据。


int strict_strtol(const char *cp, unsigned int base, long *res)
功能:将一个字符串转换sigend long型。



返回:转换成功返回0,否则返回负。


res指向转换后的signed long数据。


int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
功能:将一个字符串转换unsigend long long型。



返回:转换成功返回0,否则返回负。


res指向转换后的unsigned long long数据。


int strict_strtoll(const char *cp, unsigned int base, long long *res)
功能:将一个字符串转换sigend long long型。



返回:转换成功返回0,否则返回负。


res指向转换后的signed long long数据。


int sprintf(char *buf, const char *fmt, ...)
功能:格式化输出字符串,类似于printf,只是用字符串buf作为输出对象。



返回:返回写入buf字符串的字符个数。


int snprintf(char *buf, size_t size, const char *fmt, ...)
功能:格式化输出字符串,类似于printf,只是用字符串buf作为输出对象。


其中size为buf的大小(包括‘\0’字符)。



返回:返回写入buf字符串的字符个数。


经典:

将可变个参数(...)按照format格式化成字符串,然后将其复制到str中 (1) 如果格式化后的字符串长度 < size,则将此字符串全部复制到str中,并给其后添加一个字符串结束符('
  • char *kasprintf(gfp_t gfp, const char *fmt, ...)
  • ');
    (2) 如果格式化后的字符串长度 >= size,则只将其中的(size-1)个字符复制到str中,并给其后添加一个字符串结束符('\0'),返回值为欲写入的字符串长度。


     

    int sscanf(const char *buf, const char *fmt, ...)
    功能:格式化输入字符串,类似于scanf,只是用字符串buf作为输入对象。



    返回:返回读取buf字符串的字符个数。


    lib/kasprintf

      [+++]

    <textarea style="display: none;" readonly="readonly" name="code" class="cpp">char *kasprintf(gfp_t gfp, const char *fmt, ...)</textarea>
    char *kasprintf(gfp_t gfp, const char *fmt, ...)
    功能:格式化输出字符串到一段且gfp分配的内存中。



    返回:返回指向该内容的字符串指针。


    )
    File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
    File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
    File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
    linux中字符串转换函数 simple_strtoul_随笔_内存溢出

    linux中字符串转换函数 simple_strtoul

    linux中字符串转换函数 simple_strtoul,第1张

    linux中字符串转换函数 simple_strtoul

    Linux内核中提供的一些字符串转换函数:

    lib/vsprintf.c

    1. 1. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
    2. 2. unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
    3. 3. long simple_strtol(const char *cp, char **endp, unsigned int base)
    4. 4. long long simple_strtoll(const char *cp, char **endp, unsigned int base)
    5. 5. int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
    6. 6. int strict_strtol(const char *cp, unsigned int base, long *res)
    7. 7. int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
    8. 8. int strict_strtoll(const char *cp, unsigned int base, long long *res)
    9. 9. int sprintf(char *buf, const char *fmt, ...)
    10. 10. int snprintf(char *buf, size_t size, const char *fmt, ...)
    11. 11. int sscanf(const char *buf, const char *fmt, ...)

    unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
    功能:将一个字符串转换成unsigend long long型数据。



    返回:返回转换后数据。


    参数:cp指向字符串的开始,endp指向分析的字符串末尾的位置,base为要用的基数(进制数),base为0表示通过cp来自动判断基数,函数自动可识别的基数:‘0x’表示16进制,‘0’表示8进制,其它都认定为10进制。


    函数可转换成数字的有效字符为:[0,f]。


    举例:cp = “0x12str”,base = 0,则返回unsigned long long为18,*endp = “str”。


    参数下同。


    1. static ssize_t led_brightness_store(struct device *dev,
    2. struct device_attribute *attr, const char *buf, size_t size)
    3. {
    4. struct led_classdev *led_cdev = dev_get_drvdata(dev);
    5. ssize_t ret = -EINVAL;
    6. char *after;
    7. unsigned long state = simple_strtoul(buf, &after, 10);
    8. size_t count = after - buf;
    9. if (isspace(*after))
    10. count++;
    11. if (count == size) {
    12. ret = count;
    13. if (state == LED_OFF)
    14. led_trigger_remove(led_cdev);
    15. led_set_brightness(led_cdev, state);
    16. }
    17. return ret;
    18. }

    unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
    功能:将一个字符串转换成unsigend long型数据。



    返回:返回转换后数据。


    int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
    功能:将一个字符串转换成unsigend long型。



    返回:转换成功返回0,否则返回负。


    res指向转换后的unsigned long数据。


    说明:该函数对cp指向的字符串严格要求,cp指向的字符串必须为真正的unsigned long形式的字符串。


    字符串必须以“0x”、“0”、[0,f]开始,中间全部为有效的字符[0,f],否则返回为负。


    它会处理字符串最后的“\n”字符。


    下同

    long long simple_strtoll(const char *cp, char **endp, unsigned int base)
    功能:将一个字符串转换成sigend long long型。



    返回:返回转换后数据。


    int strict_strtol(const char *cp, unsigned int base, long *res)
    功能:将一个字符串转换sigend long型。



    返回:转换成功返回0,否则返回负。


    res指向转换后的signed long数据。


    int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
    功能:将一个字符串转换unsigend long long型。



    返回:转换成功返回0,否则返回负。


    res指向转换后的unsigned long long数据。


    int strict_strtoll(const char *cp, unsigned int base, long long *res)
    功能:将一个字符串转换sigend long long型。



    返回:转换成功返回0,否则返回负。


    res指向转换后的signed long long数据。


    int sprintf(char *buf, const char *fmt, ...)
    功能:格式化输出字符串,类似于printf,只是用字符串buf作为输出对象。



    返回:返回写入buf字符串的字符个数。


    int snprintf(char *buf, size_t size, const char *fmt, ...)
    功能:格式化输出字符串,类似于printf,只是用字符串buf作为输出对象。


    其中size为buf的大小(包括‘\0’字符)。



    返回:返回写入buf字符串的字符个数。


    经典:

    将可变个参数(...)按照format格式化成字符串,然后将其复制到str中 (1) 如果格式化后的字符串长度 < size,则将此字符串全部复制到str中,并给其后添加一个字符串结束符('
  • char *kasprintf(gfp_t gfp, const char *fmt, ...)
  • ');
    (2) 如果格式化后的字符串长度 >= size,则只将其中的(size-1)个字符复制到str中,并给其后添加一个字符串结束符('\0'),返回值为欲写入的字符串长度。


     

    int sscanf(const char *buf, const char *fmt, ...)
    功能:格式化输入字符串,类似于scanf,只是用字符串buf作为输入对象。



    返回:返回读取buf字符串的字符个数。


    lib/kasprintf

      <textarea style="display: none;" readonly="readonly" name="code" class="cpp">char *kasprintf(gfp_t gfp, const char *fmt, ...)</textarea>
      char *kasprintf(gfp_t gfp, const char *fmt, ...)
      功能:格式化输出字符串到一段且gfp分配的内存中。



      返回:返回指向该内容的字符串指针。


      欢迎分享,转载请注明来源:内存溢出

      原文地址: http://outofmemory.cn/zaji/588224.html

      (0)
      打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
      上一篇 2022-04-12
      下一篇 2022-04-12

      发表评论

      登录后才能评论

      评论列表(0条)

      保存