function

long int strtol (const char* str, char** endptr, int base); —— Convert string to long integer

long long int strtoll (const char* str, char** endptr, int base); —— Convert string to long long integer

unsigned long int strtoul (const char* str, char** endptr, int base); —— Convert string to unsigned long integer

unsigned long long int strtoull (const char* str, char** endptr, int base); —— Convert string to unsigned long long integer

Parses the C-string str interpreting its content as an integral number of the specified base, which is returned as a long int / long long int / unsigned long int / unsigned long long int value. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number.

The function first discards as many whitespace characters as necessary
until the first non-whitespace character is found. Then, starting from
this character, takes as many characters as possible that are valid
following a syntax that depends on the base parameter, and
interprets them as a numerical value. Finally, a pointer to the first
character following the integer representation in str is stored in the object pointed by endptr.

If the value of base is zero, the syntax expected is similar to that of integer constants, which is formed by a succession of:

  • An optional sign character (+ or -)
  • An optional prefix indicating octal or hexadecimal base ("0" or "0x"/"0X" respectively)
  • A sequence of decimal digits (if no base prefix was specified) or
    either octal or hexadecimal digits if a specific prefix is present

If the base value is between 2 and 36, the format expected
for the integral number is a succession of any of the valid digits
and/or letters needed to represent integers of the specified radix
(starting from '0' and up to 'z'/'Z' for radix 36). The sequence may optionally be preceded by a sign (either + or -) and, if base is 16, an optional "0x" or "0X" prefix.

If the first sequence of non-whitespace characters in str is not a valid integral number as defined above, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

For locales other than the "C" locale, additional subject sequence forms may be accepted.

Parameters

str
C-string beginning with the representation of an integral number.
endptr
Reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value.
This parameter can also be a null pointer, in which case it is not used.
base
Numerical base (radix) that determines the valid characters and their interpretation.
If this is 0, the base used is determined by the format in the sequence (see above).

Return Value

On success, the function returns the converted integral number as a long int / long long int / unsigned long int / unsigned long long int value.
If no valid conversion could be performed, a zero value is returned (0L / 0LL / 0UL / 0ULL).
If the value read is out of the range of representable values by a long int / long long int / unsigned long int / unsigned long long int, the function returnsLONG_MAX orLONG_MIN  /LLONG_MAX orLLONG_MIN / ULONG_MAX /ULLONG_MAX (defined in<climits>), and errno is set to ERANGE.

examples

 /* strtol example */
#include <stdio.h> /* printf */
#include <stdlib.h> /* strtol */ int main ()
{
char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
char * pEnd;
long int li1, li2, li3, li4;
li1 = strtol (szNumbers,&pEnd,);
li2 = strtol (pEnd,&pEnd,);
li3 = strtol (pEnd,&pEnd,);
li4 = strtol (pEnd,NULL,);
printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", li1, li2, li3, li4);
return ;
}

运行结果:

The decimal equivalents are: 2001, 6340800, -3624224 and 7340031
 /* strtoll example */
#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* strtoll */ int main ()
{
char szNumbers[] = "1856892505 17b00a12b -01100011010110000010001101100 0x6fffff";
char* pEnd;
long long int lli1, lli2, lli3, lli4;
lli1 = strtoll (szNumbers, &pEnd, );
lli2 = strtoll (pEnd, &pEnd, );
lli3 = strtoll (pEnd, &pEnd, );
lli4 = strtoll (pEnd, NULL, );
printf ("The decimal equivalents are: %lld, %lld, %lld and %lld.\n", lli1, lli2, lli3, lli4);
return ;
}

运行结果:

The decimal equivalents are: 1856892505, 6358606123, -208340076 and 7340031
 /* strtoul example */
#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* strtoul */ int main ()
{
char buffer [];
unsigned long ul;
printf ("Enter an unsigned number: ");
fgets (buffer, , stdin);
ul = strtoul (buffer, NULL, );
printf ("Value entered: %lu. Its double: %lu\n",ul,ul*);
return ;
}

运行结果:

Enter an unsigned number: 30003
Value entered: 30003. Its double: 60006
 /* strtoull example */
#include <stdio.h> /* printf, NULL */
#include <stdlib.h> /* strtoull */ int main ()
{
char szNumbers[] = "250068492 7b06af00 1100011011110101010001100000 0x6fffff";
char * pEnd;
unsigned long long int ulli1, ulli2, ulli3, ulli4;
ulli1 = strtoull (szNumbers, &pEnd, );
ulli2 = strtoull (pEnd, &pEnd, );
ulli3 = strtoull (pEnd, &pEnd, );
ulli4 = strtoull (pEnd, NULL, );
printf ("The decimal equivalents are: %llu, %llu, %llu and %llu.\n", ulli1, ulli2, ulli3, ulli4);
return ;
}

运行结果:

The decimal equivalents are: 250068492, 2064035584, 208622688 and 7340031.

封装

     bool stringToI32(const std::string &str, int32_t  &val)
{
long temp_val = ;
bool isOK = stringToL(str, temp_val);
val = temp_val;
return isOK && (temp_val >= -0x7fffffff && temp_val <= 0x7fffffff/*32bit整形的有效范围*/);
} bool stringToU32(const std::string &str, uint32_t &val)
{
unsigned long temp_val = ;
bool isOK = stringToUL(str, temp_val);
val = temp_val;
return isOK && (temp_val <= 0xffffffff/*32bit无符号整形的有效范围*/);
} bool stringToI64(const std::string &str, int64_t &val)
{
long long temp_val = ;
bool isOK = stringToLL(str, temp_val);
val = temp_val;
return isOK && (temp_val >= -0x7fffffffffffffff && temp_val <= 0x7fffffffffffffff/*64bit整形的有效范围*/);
} bool stringToU64(const std::string &str, uint64_t &val)
{
unsigned long long temp_val = ;
bool isOK = stringToULL(str, temp_val);
val = temp_val;
return isOK && (temp_val <= 0xffffffffffffffff/*64bit无符号整形的有效范围*/);
} bool stringToL(const std::string &str, long &val)
{
bool isOK = false;
const char *nptr = str.c_str();
char *endptr = NULL;
errno = ;
val = strtol(nptr, &endptr, );
//error ocur
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
|| (errno != && val == ))
{ }
//no digit find
else if (endptr == nptr)
{ }
else if (*endptr != '\0')
{
// printf("Further characters after number: %s\n", endptr);
}
else
{
isOK = true;
} return isOK;
} bool stringToLL(const std::string &str, long long &val)
{
bool isOK = false;
const char *nptr = str.c_str();
char *endptr = NULL;
errno = ;
val = strtoll(nptr, &endptr, );
//error ocur
if ((errno == ERANGE && (val == LLONG_MAX || val == LLONG_MIN))
|| (errno != && val == ))
{ }
//no digit find
else if (endptr == nptr)
{ }
else if (*endptr != '\0')
{
// printf("Further characters after number: %s\n", endptr);
}
else
{
isOK = true;
} return isOK;
} bool stringToUL(const std::string &str, unsigned long &val)
{
bool isOK = false;
const char *nptr = str.c_str();
char *endptr = NULL;
errno = ;
val = strtoul(nptr, &endptr, );
//error ocur
if ((errno == ERANGE && (val == ULONG_MAX))
|| (errno != && val == ))
{ }
//no digit find
else if (endptr == nptr)
{ }
else if (*endptr != '\0')
{
// printf("Further characters after number: %s\n", endptr);
}
else
{
isOK = true;
} return isOK;
} bool stringToULL(const std::string &str, unsigned long long &val)
{
bool isOK = false;
const char *nptr = str.c_str();
char *endptr = NULL;
errno = ;
val = strtoull(nptr, &endptr, );
//error ocur
if ((errno == ERANGE && (val == ULLONG_MAX))
|| (errno != && val == ))
{ }
//no digit find
else if (endptr == nptr)
{ }
else if (*endptr != '\0')
{
// printf("Further characters after number: %s\n", endptr);
}
else
{
isOK = true;
} return isOK;
}

本文参考自:

http://www.cplusplus.com/reference/cstdlib/strtol/

http://www.cplusplus.com/reference/cstdlib/strtoll/

http://www.cplusplus.com/reference/cstdlib/strtoul/

http://www.cplusplus.com/reference/cstdlib/strtoull/

http://blog.csdn.net/ywy2090/article/details/64918801

strtol / strtoll / strtoul / strtoull的更多相关文章

  1. strtol,strtoll,strtoul, strtoull字符串转化成数字

      今天看kafka,有一个参数选项中有: 'S'   seq=strtoull(optarg,NULL,10); do_seq=1; 之后查找了下 strtoull 函数的功能,了解如下: ---- ...

  2. 函数atof,atoi,atol,strtod,strtol,strtoul 描述

    函数atof,atoi,atol,strtod,strtol,strtoul atof(将字串转换成浮点型数) 相关函数 atoi,atol,strtod,strtol,strtoul表头文件 #in ...

  3. atof,atoi,atol,strtod,strtol,strtoul

    字符串处理函数 atof 将字串转换成浮点型数 atoi 字符串转换成整型数 atol 函数名: atol 功 能: 把字符串转换成长整型数 用 法: long atol(const char *np ...

  4. strtoull函数的使用,及相关信息汇总

    kafka中涉及到序列化,有一个参数是在发送消息实体前边以一个序列号打头, seq = strtoull((prdcfgval+1), NULL, 10); do_seq = 1; 其中(prdcfg ...

  5. C和C++字符串处理整理

    在刷leetcode题目的过程中,发现自己对于c和c++字符串的处理并不是很拿手,处理起来比较费劲,而且,算法题似乎很中意字符串的处理,有很多题目都涉及到它.字符串处理比较基础,但是很重要,因此,整理 ...

  6. 调试 & 常数优化:我有特别的 debug 技巧

    rxz 的调试技巧(https://www.zhihu.com/question/60719584/answer/179363450): #define DEBUG printf("Pass ...

  7. 浅析C语言中strtol()函数与strtoul()函数的用法

    转自:http://www.jb51.net/article/71463.htm C语言strtol()函数:将字符串转换成long(长整型数) 头文件: ? 1 #include <stdli ...

  8. 字符串转换atof atoi atol gcvt strtod strtol strto ul toascii tolower toupper

    atof(将字符串转换成浮点型数) 相关函数 atoi,atol,strtod,strtol,strtoul 表头文件 #include <stdlib.h> 定义函数 double at ...

  9. C++字符串转整形、浮点型stof()、atoi()、strtol()等

    头文件:#include<stdlib.h>string str;stof:float val=stof(str);atoi:int val=atoi(str);atol:long val ...

随机推荐

  1. ylbtech-LanguageSamples-Versioning(版本控制)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Versioning(版本控制) 1.A,示例(Sample) 返回顶部 “版本控制”示 ...

  2. jquery获取一组相同标签内没有class的标签

    $("ul>li[class!='pre'][class!='nex']").each(function(i){ $(this).html(i); });

  3. jquery中获取相邻元素相关的命令:next()、prev()和siblings()

    jquery里我们要获取某个元素的相邻元素时,可以用到的命令有三个: next():用来获取下一个同辈元素. prev():用来获取上一个同辈元素. siblings():用来获取所有的同辈元素. 下 ...

  4. 《深入理解Java虚拟机》笔记7

    组内有人问我,她写的程序总是在短时间内就jvm异常. 另外,debug时候又可以正常通过,写的逻辑并不复杂, 只是用poi检索Excel.第一反应还是程序可能写的有问题, 也许写了一个jvm未预测的错 ...

  5. 使用git-svn迁移SVN至GitLab

    使用git-svn迁移SVN至GitLab 1.安装git和git-svn 后面的步骤中对git版本有一定要求,通过yum安装的git版本较低,这里进行编译安装 [root@DevTest ~]# y ...

  6. (转)dubbo design

    框架设计 整体设计 图例说明: 图中左边淡蓝背景的为服务消费方使用的接口,右边淡绿色背景的为服务提供方使用的接口, 位于中轴线上的为双方都用到的接口. 图中从下至上分为十层,各层均为单向依赖,右边的黑 ...

  7. 云计算之路-试用Azure:如何建立虚拟机之间的内网连接

    在阿里云上,同一个帐户创建的所有虚拟机(云服务器)之间的内网是直接连通的.而Azure则完全不一样,一开始使用时有点不知所措,后来摸索出来了——在Azure中只有处于同一个虚拟网络(Virtual N ...

  8. PHP实现程序单例执行

    原创文章,转载请注明出处:http://huyanping.sinaapp.com/?p=222 作者:Jenner 一.场景描写叙述: 近期我们一块业务.须要不断的监听一个文件夹的变化.假设文件夹中 ...

  9. linux内核及其模块的查询,加载,卸载 lsusb等

    http://blog.sina.com.cn/s/blog_53e81e2a0100zkxi.html 1,/sbin/update-modules文件,他是一个linux通用的模块管理脚本程序. ...

  10. ant 重置(修改)DatePicker MonthPicker Cascader 的值

    1.清空值 this.props.form.resetFields(); 2.监听 props 的变化,触发 清空按钮 .