命名空间
变体
操作

std::wcstoimax, std::wcstoumax

来自 cppreference.cn
< cpp‎ | string‎ | wide
定义于头文件 <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) 表示八进制基数 (仅当基数为 80 时适用)
  • (可选) 前缀 (0x0X) 表示十六进制基数 (仅当基数为 160 时适用)
  • 一系列数字

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

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

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

如果减号是输入序列的一部分,则从数字序列计算出的数值将被取反,如同结果类型中的一元减号,这将应用无符号整数环绕规则。

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

目录

[编辑] 参数

nptr - 指向要解释的以 null 结尾的宽字符串的指针
endptr - 指向宽字符指针的指针
base - 被解释整数值的*基数*

[编辑] 返回值

成功时,返回对应于 str 内容的整数值。如果转换后的值超出相应返回类型的范围,则发生范围错误并返回 INTMAX_MAXINTMAX_MINUINTMAX_MAX0(视情况而定)。如果无法执行转换,则返回 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_tstd::uintmax_t
(函数) [编辑]
将宽字符串转换为整数值
(函数) [编辑]
将宽字符串转换为无符号整数值
(函数) [编辑]
C 文档 用于 wcstoimax, wcstoumax