C/C++ wcstoimax() 和 wcstoumax() 函数

2024 年 8 月 29 日 | 4 分钟阅读

C/C++ 中的 wcstoimax()wcstoumax() 函数与 C++ 中的 strtoimax()strtoumax() 函数以相同的方式操作,不同之处在于它们用于将宽字符串 (wstring) 的数据转换为给定基数的整数。此函数定义在头文件 cinttypes 中。

头文件

wcstoimax() 方法

wcstoimax()“宽字符字符串转换为最大整数” 的缩写。它将宽字符字符串转换为类型为 intmax_t 的数字最大宽度有符号整数。它可以根据输入文本的格式转换各种基数(例如,十进制、十六进制、八进制)。

语法

它具有以下语法:

参数

npt: 要转换为宽字符字符串的数字。

endpt: 一个指向宽字符的指针,包含表示函数在给定输入字符串中第一个不正确字符的地址。

bases: 数字系统的基数(例如,十进制为 10,十六进制为 16,等等)。

返回值

该函数返回两个值,如下所示

  • 如果转换正确,函数将返回结果整数值。
  • 如果无法进行正确转换,则返回零 (0) 值。

示例

输出

Programming in base 36 is 94215099711813100 in base 10

The given input String is = Programming
The Number with the base 30 in the given string 15309807264430906 in base 10
the end of the string points to 

wcstoumax() 方法

wcstoumax() 方法将宽字符字符串 npt 转换为整数类型 uintmax_t。基本输入值为 0,范围为 2-36。wcstoumax() 函数与 wcstoul() 和 wcstoull() 函数相同。唯一的区别是返回的值对应于 uintmax_t 类型。

语法

它具有以下语法:

参数

npt: 要转换为宽字符字符串的数字。

endpt: 它是一个指针,包含宽字符,其中包含表示函数在给定输入字符串中第一个不正确字符的地址。

bases: 数字系统的基数(例如,十进制为 10,十六进制为 16,等等)。

示例

输出

The given input String = 123abc
The Value stored in the number is: 123
The end of the string points to abc

The given input String = abcdeed
The Value stored in the number is: 0
The end of the string points to abcdeed

wcstoimax() 和 wcstoumax() 函数的用途

  • 在处理多基数最大宽度有符号整数时,使用 wcstoimax() 将表示数字的宽字符序列转换为 intmax_t 类型。
  • 在处理多基数最大宽度无符号整数时,使用 wcstoumax() 将表示整数的宽字符字符串转换为 uintmax_t 类型。