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如果发生溢出或下溢。

关于此代码以及其他返回代码的详细信息,请参阅 _doserrno, errno, _sys_errlist, and _sys_nerr 。

备注

其中每个函数将转换输入的字符串strSource无符号

strtoul停止读取字符串strSource不能将其识别为一个数字部分的第一个字符。 这可能是终止 null 字符,也可能是第一个数字的字符大于或等于。 

LC_NUMERIC类别设置的区域设置确定基数字符在识别strSource; 有关详细信息,请参阅setlocale

 strtoulwcstoul使用当前区域设置;_strtoul_l和 _wcstoul_l是相同,只不过它们使用传递的区域设置。 有关详细信息,请参阅 Locale

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

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

一般文本例程映射

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

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

[空白] [{+ | -}] [0 [{ xX }]] [数字字母]

空白可能包含空格和制表符字符,将被忽略。 数字是一个或多个十进制数字。 字母中的一个或多个字母 a 到 z (或 A 至 Z)。 不符合此形式的第一个字符停止扫描。 

如果为介于 2 和 36,之间,则它可作为数字的基数。 

如果为 0,通过指向的字符串的初始字符strSource用于确定基。 如果第一个字符为 0,且第二个字符不为“x”或“X”,则将该字符串视为八进制整数。 如果第一个字符为“0”,且第二个字符为“x”或“X”,则将该字符串视为十六进制整数。 如果第一个字符是“1”至“9”,则将该字符串视为十进制整数。 为字母“a”到“z”(或“A”到“Z”)分配了 10 到 35 的值;仅允许分配的值小于 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_l、wcstod、_wcstod_l
strtol、wcstol、_strtol_l、_wcstol_l
atof、_atof_l、_wtof、_wtof_l

猜你喜欢

转载自blog.csdn.net/qq_28382661/article/details/81192653
l