strtoul

strtoul

将字符串转换成无符号长整型数

相关函数

atof,atoi,atol,strtod,strtol

表头文件

#include <stdlib.h>

定义函数

unsigned long int strtoul(const char *nptr, char **endptr, int base);

函数说明

strtoul()会将参数nptr字符串根据参数base来转换成无符号的长整型数。参数base范围从2至36,或0。参数base代表采用的进制方式,如base值为10则采用10进制,若base值为16则采用16进制数等。当base值为0时则是采用10进制做转换,但遇到如'0x'前置字符则会使用16进制做转换。一开始strtoul()会扫描参数nptr字符串,跳过前面的空格字符串,直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时('\0')结束转换,并将结果返回。若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr返回。

+

返回值

返回转换后的长整型数,否则返回ERANGE并将错误代码存入errno中。

附加说明

ERANGE指定的转换字符串超出合法范围。

范例

参考strtol()  

man

STRTOUL(3)                                Linux Programmer's Manual                               STRTOUL(3)

NAME
       strtoul, strtoull, strtouq - convert a string to an unsigned long integer

SYNOPSIS
       #include <stdlib.h>

       unsigned long int strtoul(const char *nptr, char **endptr, int base);

       unsigned long long int strtoull(const char *nptr, char **endptr,
                                       int base);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       strtoull():
           _XOPEN_SOURCE >= 600 || _BSD_SOURCE || _SVID_SOURCE || _ISOC99_SOURCE ||
           _POSIX_C_SOURCE >= 200112L;
           or cc -std=c99

DESCRIPTION
       The strtoul() function converts the initial part of the string in nptr to an unsigned long int  value
       according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.

       The  string  may begin with an arbitrary amount of white space (as determined by isspace(3)) followed
       by a single optional '+' or '-' sign.  If base is zero or 16, the string may then include a "0x" pre‐
       fix,  and  the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal) unless
       the next character is '0', in which case it is taken as 8 (octal).

       The remainder of the string is converted to an unsigned long int value in the obvious  manner,  stop‐
       ping  at  the  first character which is not a valid digit in the given base.  (In bases above 10, the
       letter 'A' in either uppercase or lowercase represents 10, 'B' represents 11, and so forth, with  'Z'
       representing 35.)

       If  endptr  is  not NULL, strtoul() stores the address of the first invalid character in *endptr.  If
       there were no digits at all, strtoul() stores the original value of nptr in *endptr (and returns  0).
       In particular, if *nptr is not '\0' but **endptr is '\0' on return, the entire string is valid.

       The  strtoull() function works just like the strtoul() function but returns an unsigned long long int
       value.

RETURN VALUE
       The strtoul() function returns either the result of the conversion or, if there was a  leading  minus
       sign, the negation of the result of the conversion represented as an unsigned value, unless the orig‐
       inal (nonnegated) value would overflow; in the latter case,  strtoul()  returns  ULONG_MAX  and  sets
       errno to ERANGE.  Precisely the same holds for strtoull() (with ULLONG_MAX instead of ULONG_MAX).

ERRORS
       EINVAL (not in C99) The given base contains an unsupported value.

       ERANGE The resulting value was out of range.

       The  implementation may also set errno to EINVAL in case no conversion was performed (no digits seen,
       and 0 returned).

ATTRIBUTES
       For an explanation of the terms used in this section, see attributes(7).

       ┌─────────────────────────────────┬───────────────┬────────────────┐
       │Interface                        │ Attribute     │ Value          │
       ├─────────────────────────────────┼───────────────┼────────────────┤
       │strtoul(), strtoull(), strtouq() │ Thread safety │ MT-Safe locale │
       └─────────────────────────────────┴───────────────┴────────────────┘
CONFORMING TO
       strtoul(): POSIX.1-2001, POSIX.1-2008, C89, C99 SVr4.

       strtoull(): POSIX.1-2001, POSIX.1-2008, C99.

NOTES
       Since strtoul() can legitimately return 0 or ULONG_MAX (ULLONG_MAX for strtoull())  on  both  success
       and  failure,  the  calling  program  should set errno to 0 before the call, and then determine if an
       error occurred by checking whether errno has a nonzero value after the call.

       In locales other than the "C" locale, other strings may be accepted.   (For  example,  the  thousands
       separator of the current locale may be supported.)

       BSD also has

           u_quad_t strtouq(const char *nptr, char **endptr, int base);

       with  completely  analogous  definition.  Depending on the wordsize of the current architecture, this
       may be equivalent to strtoull() or to strtoul().

       Negative values are considered valid input and are silently converted to the equivalent unsigned long
       int value.

EXAMPLE
       See  the example on the strtol(3) manual page; the use of the functions described in this manual page
       is similar.

SEE ALSO
       atof(3), atoi(3), atol(3), strtod(3), strtol(3)

COLOPHON
       This page is part of release 4.04 of the Linux man-pages project.   A  description  of  the  project,
       information   about  reporting  bugs,  and  the  latest  version  of  this  page,  can  be  found  at
       http://www.kernel.org/doc/man-pages/.

GNU                                              2015-08-08                                       STRTOUL(3)

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值