Namespaces
Variants

std:: wcstoimax, std:: wcstoumax

From cppreference.net
定义于头文件 <cinttypes>
std:: intmax_t wcstoimax ( const wchar_t * nptr, wchar_t ** endptr, int base ) ;
(C++11 起)
std:: uintmax_t wcstoumax ( const wchar_t * nptr, wchar_t ** endptr, int base ) ;
(C++11 起)

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

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

  • (可选) 正负号
  • (可选) 表示八进制基数的前缀 ( 0 )(仅当基数为 8 0 时适用)
  • (可选) 表示十六进制基数的前缀 ( 0x 0X )(仅当基数为 16 0 时适用)
  • 数字序列

基数的有效取值范围为 {0, 2, 3, ..., 36} 。对于基数为 2 的整数,有效数字集为 {0, 1} ;对于基数为 3 的整数,有效数字集为 {0, 1, 2} ,依此类推。对于大于 10 的基数,有效数字包含字母字符,从基数为 11 的整数对应的 Aa 开始,直至基数为 36 的整数对应的 Zz 。字符的大小写将被忽略。

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

如果 base 的值为 0 ,将自动检测数值进制:若前缀为 0 则为八进制,若前缀为 0x 0X 则为十六进制,否则为十进制。

如果减号是输入序列的一部分,则从数字序列计算得出的数值将被取反,如同在结果类型中通过 一元减号 运算符处理,该操作遵循无符号整数回绕规则。

该函数将 endptr 指向的指针设置为指向最后一个被解释宽字符之后的位置。如果 endptr 是空指针,则会被忽略。

目录

参数

nptr - 指向待解析的空终止宽字符串的指针
endptr - 指向宽字符指针的指针
base - 被解析整数值的 基数

返回值

成功时返回与 str 内容对应的整数值。若转换值超出对应返回类型的范围,将发生范围错误并返回 INTMAX_MAX INTMAX_MIN UINTMAX_MAX 0 (视具体情况而定)。若无法执行任何转换,则返回 0

示例

#include <cinttypes>
#include <iostream>
#include <string>
int main()
{
    std::wstring str = L"helloworld";
    std::intmax_t val = std::wcstoimax(str.c_str(), nullptr, 36);
    std::wcout << str << " in base 36 is " << val << " in base 10\n";
    wchar_t* nptr;
    val = std::wcstoimax(str.c_str(), &nptr, 30);
    if (nptr != &str[0] + str.size())
        std::wcout << str << " in base 30 is invalid."
                   << " The first invalid digit is " << *nptr << '\n';
}

输出:

helloworld in base 36 is 1767707668033969 in base 10
helloworld in base 30 is invalid. The first invalid digit is w

参见

(C++11) (C++11)
将字节字符串转换为 std::intmax_t std::uintmax_t
(函数)
将宽字符串转换为整数值
(函数)
将宽字符串转换为无符号整数值
(函数)
C 文档 关于 wcstoimax , wcstoumax