命名空间
变体
操作

wcstoimax, wcstoumax

来自 cppreference.cn
< c‎ | string‎ | wide
定义于头文件 <inttypes.h>
intmax_t wcstoimax( const wchar_t *restrict nptr,
                    wchar_t **restrict endptr, int base );
(C99 起)
uintmax_t wcstoumax( const wchar_t *restrict nptr,
                     wchar_t **restrict endptr, int base );
(C99 起)

nptr 指向的宽字符串解释为无符号整数值。

丢弃任何空白字符(通过调用 iswspace 识别),直到找到第一个非空白字符,然后尽可能多地获取字符以形成有效的 base-n(其中 n=base)无符号整数数字表示,并将它们转换为整数值。有效的无符号整数值由以下部分组成

  • (可选) 加号或减号
  • (可选) 前缀 (0) 表示八进制基数(仅当基数为 80 时适用)
  • (可选) 前缀 (0x0X) 表示十六进制基数(仅当基数为 160 时适用)
  • 数字序列

base 的有效值集合为 {0, 2, 3, ..., 36}。base-2 整数的有效数字集合为 {0, 1},base-3 整数的有效数字集合为 {0, 1, 2},依此类推。对于大于 10 的基数,有效数字包括字母字符,从 base-11 整数的 Aa 开始,到 base-36 整数的 Zz。字符的大小写被忽略。

当前安装的 C locale 可能会接受其他数字格式。

如果 base 的值为 0,则自动检测数字基数:如果前缀为 0,则基数为八进制;如果前缀为 0x0X,则基数为十六进制;否则基数为十进制。

如果输入序列中包含减号,则从数字序列计算出的数值将像结果类型中的一元减号一样被取反,这适用无符号整数环绕规则。

这些函数将 endptr 指向的指针设置为指向已解释的最后一个字符之后的宽字符。如果 endptr 是空指针,则忽略它。

目录

[编辑] 参数

nptr - 指向要解释的空终止宽字符串的指针
endptr - 指向宽字符指针的指针。
base - 已解释整数值的基数

[编辑] 返回值

成功时,返回与 str 内容对应的整数值。如果转换后的值超出相应返回类型的范围,则会发生范围错误,并返回 INTMAX_MAXINTMAX_MINUINTMAX_MAX0(视情况而定)。如果无法执行转换,则返回 0

[编辑] 示例

#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
 
int main(void)
{
  wchar_t* endptr;
 
  wprintf(L"%ld\n", wcstoimax(L" -123junk", &endptr, 10)); /* base 10                    */
  wprintf(L"%ld\n", wcstoimax(L"11111111", &endptr, 2));   /* base 2                     */
  wprintf(L"%ld\n", wcstoimax(L"XyZ", &endptr, 36));       /* base 36                    */
  wprintf(L"%ld\n", wcstoimax(L"010", &endptr, 0));        /* octal auto-detection       */
  wprintf(L"%ld\n", wcstoimax(L"10", &endptr, 0));         /* decimal auto-detection     */
  wprintf(L"%ld\n", wcstoimax(L"0x10", &endptr, 0));       /* hexadecimal auto-detection */
 
  /* range error             */
  /* LONG_MAX+1 --> LONG_MAX */
  errno = 0;
  wprintf(L"%ld\n", wcstoimax(L"9223372036854775808", &endptr, 10));
  wprintf(L"%s\n", strerror(errno));
}

输出

-123
255
44027
8
10
16
9223372036854775807
Numerical result out of range

[编辑] 参考文献

  • C11 标准 (ISO/IEC 9899:2011)
  • 7.8.2.4 wcstoimax 和 wcstoumax 函数 (p: 220)
  • C99 标准 (ISO/IEC 9899:1999)
  • 7.8.2.4 wcstoimax 和 wcstoumax 函数 (p: 201)

[编辑] 参见

将字节字符串转换为 intmax_tuintmax_t
(函数) [编辑]
(C95)(C99)
将宽字符串转换为整数值
(函数) [编辑]
将宽字符串转换为无符号整数值
(函数) [编辑]
C++ 文档 关于 wcstoimax, wcstoumax