strtoul, _strtoul_l, wcstoul, _wcstoul_l

将字符串转换为无符号的长整数值。

语法

unsigned long strtoul(
   const char *strSource,
   char **endptr,
   int base
);
unsigned long _strtoul_l(
   const char *strSource,
   char **endptr,
   int base,
   _locale_t locale
);
unsigned long wcstoul(
   const wchar_t *strSource,
   wchar_t **endptr,
   int base
);
unsigned long _wcstoul_l(
   const wchar_t *strSource,
   wchar_t **endptr,
   int base,
   _locale_t locale
);

参数

strSource
要转换的 null 终止的字符串。

endptr
指向停止扫描的字符的指针。

base
要使用的基数。

locale
要使用的区域设置。

返回值

strtoul 返回转换后的值(如果有),或溢出的 ULONG_MAX。 如果无法执行任何转换,则 strtoul 返回 0。 wcstoul 返回类似于 strtoul 的值。 对于这两个函数,如果出现溢出或下溢,则 errno 设置为 ERANGE

有关返回代码的详细信息,请参阅 errno_doserrno_sys_errlist_sys_nerr

注解

这些函数均将输入字符串 strSource 转换为 unsigned long

strtoul 在首个无法识别为数字一部分的字符处停止读取字符串 strSource。 此字符可能是终止 NULL 字符,也可能是大于或等于 base 的第一个数字字符。 区域设置的 LC_NUMERIC 类别设置确定 strSource 中的基数字符的识别;有关详细信息,请参阅 setlocalestrtoulwcstoul 使用当前区域设置;_strtoul_l_wcstoul_l 相同,只不过它们改用传入的区域设置。 有关详细信息,请参阅 Locale

如果 endptr 不为 NULL,则在 endptr 所指向的位置存储指向停止扫描的字符的指针。 如果无法执行任何转换(未找到任何有效的数字或指定了无效的基数),则将 strSource 的值存储在由 endptr 指向的位置。

wcstoulstrtoul 的宽字符版本;它的 strSource 参数是宽字符字符串。 否则,这些函数具有相同行为。

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

一般文本例程映射

TCHAR.H 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_tcstoul strtoul strtoul wcstoul
_tcstoul_l strtoul_l _strtoul_l _wcstoul_l

strtoul 需要 strSource 指向以下形式的字符串:

[whitespace] [{+ | -}] [0 [{ x | X }]] [digits | letters]

whitespace 可能包含被忽略的空格和制表符。 digits 是一个或多个十进制数字。 lettersaz(或 AZ)的一个或多个字母。 不符合此形式的第一个字符将停止扫描。 如果 base 介于 2 和 36 之间,则将它用作数字的基数。 如果 base 为 0,则由 strSource 指向的字符串的初始字符用于确定基数。 如果第一个字符为 0,且第二个字符不为 xX,则将该字符串视为八进制整数。 如果第一个字符为“0”,且第二个字符为 xX,则将该字符串视为十六进制整数。 如果第一个字符是“1”至“9”,则将该字符串视为十进制整数。 为字母 az(或 AZ)分配了 10 到 35 的值;仅允许分配的值小于 base 的字母。 超出基数范围的第一个字符停止扫描。 例如,如果 base 为 0 且扫描的第一个字符为“0”,则假定为八进制整数,且“8”或“9”字符将停止扫描。 strtoul 允许加号 (+) 或减号 (-) 符号前缀;前导减号符号表示返回值不起作用。

要求

例程 必需的标头
strtoul <stdlib.h>
wcstoul <stdlib.h><wchar.h>
_strtoul_l <stdlib.h>
_wcstoul_l <stdlib.h><wchar.h>

有关兼容性的详细信息,请参阅 兼容性

示例

请参阅 strtod 的示例。

另请参阅

数据转换
区域设置
localeconv
setlocale_wsetlocale
字符串到数值函数
strtod_strtod_lwcstod_wcstod_l
strtolwcstol_strtol_l_wcstol_l
atof_atof_l_wtof_wtof_l