名稱空間
變體
操作

wcstoimax, wcstoumax

來自 cppreference.com
< 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 識別),直到找到第一個非空白字元,然後儘可能多地獲取字元以形成有效的 n 進位制(其中 n=base)無符號整數表示,並將其轉換為整數值。有效的無符號整數值由以下部分組成:

  • (可選) 加號或減號
  • (可選) 字首 (0) 表示八進位制基數(僅當基數為 80 時適用)
  • (可選) 字首 (0x0X) 表示十六進位制基數(僅當基數為 160 時適用)
  • 一系列數字

基數的有效值集合是 {0, 2, 3, ..., 36}。基數-2 整數的有效數字集合是 {0, 1},基數-3 整數的有效數字集合是 {0, 1, 2},以此類推。對於大於 10 的基數,有效數字包括字母字元,從基數-11 整數的 Aa 到基數-36 整數的 Zz。字元的大小寫被忽略。

當前安裝的 C 區域設定可能接受其他數字格式。

如果 base 的值為 0,則自動檢測數字基數:如果字首是 0,則基數是八進位制;如果字首是 0x0X,則基數是十六進位制;否則基數是十進位制。

如果減號是輸入序列的一部分,則從數字序列計算出的數值將被取反,如同結果型別中的一元減號一樣,這將應用無符號整數環繞規則。

函式將 endptr 指向的指標設定為指向已解釋的最後一個字元之後的寬字元。如果 endptr 是空指標,則忽略它。

目錄

[編輯] 引數

nptr - 指向要解釋的以 null 結尾的寬字串的指標
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