C++ 中的wcstoull() 函數將寬字符串的內容解釋為指定基數的無符號整數,並將其值返回為無符號長 long int.
wcstoull() 函數還設置一個指針,指向寬字符串的最後一個有效字符(如果有)之後的第一個字符,否則該指針設置為空。
For base 10 and the wide string L"29hi$" Valid numeric part -> 29 First character after valid numeric part -> h
它在<cwchar> 頭文件中定義。
wcstoull()原型
unsigned long wcstoull( const wchar_t* str, wchar_t** str_end, int base );
wcstoull() 函數將寬字符串 str
、指向寬字符 str_end
的指針和整數值 - base
作為其參數。
然後它將寬字符串的內容解釋為給定 base
的無符號整數,並返回一個無符號 long long int 值。
參數:
str
:具有無符號整數表示的寬字符串。str_end
:指向寬字符指針的指針。 str_end 的值由函數設置為 str 中最後一個有效字符之後的下一個字符。該參數也可以是空指針,在這種情況下不使用。base
:整數值的基數。 base 的有效值集是 {0, 2, 3, ..., 35, 36}。
返回:
wcstoull() 函數返回:
- 一個無符號的 long long int 值(從字符串轉換而來)。
- 如果無法執行有效轉換,則為 0。
示例 1:wcstoull() 函數如何工作?
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t str1[] = L"101aa\u16b6";
wchar_t str2[] = L"59";
wchar_t *end;
unsigned long long value;
int base = 10;
value = wcstoull(str1, &end, base);
wcout << L"String value = " << str1 << endl;
wcout << L"Unsigned Long Long Int value = " << value << endl;
wcout << L"End String = " << end << endl;
value = wcstoull(str2, &end, base);
wcout << L"String value = " << str2 << endl;
wcout << L"Unsigned Long Long Int value = " << value << endl;
wcout << L"End String = " << end << endl;
return 0;
}
運行程序時,輸出將是:
String value = 101aaᚶ Unsigned Long Long Int value = 101 End String = aaᚶ String value = 59 Unsigned Long Long Int value = 59 End String =
wcstoull() 函數的有效整數值包括:
- 可選的 + 或 - 符號。
- 八進製基數的前綴 0(僅在基數 = 8 或 0 時適用)。
- 十六進製基數的前綴 0x 或 0X(僅在基數 = 16 或 0 時適用)。
- 一係列數字和/或字母(如果基數大於 10)。
如果參數在開頭包含減號 (-),則負數會隱式轉換為 unsigned long long int 類型,即正數。
參數 base 的有效值為 {0, 2, 3, ..., 35, 36}。以 2 為底的一組有效數字為 {0, 1},以 3 為底的一組有效數字為 {0, 1, 2},依此類推。對於從 11 到 36 的堿基,有效數字包括字母。
以 11 為底的有效數字集是 {0, 1, ..., 9, A, a},以 12 為底的有效數字是 {0, 1, ..., 9, A, a, B, b} 等等。
示例 2:wcstoull() 具有不同基數的函數
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t *end;
wchar_t str[] = L"311bz\u03fe\u03ff";
wcout << str << L" to Unsigned Long Long Int with base-5 = " << wcstoull(str, &end, 5) << endl;
wcout << L"End String = " << end << endl << endl;
wcout << str << L" to Unsigned Long Long Int with base-12 = " << wcstoull(str, &end, 12) << endl;
wcout << L"End String = " << end << endl << endl;
wcout << str << L" to Unsigned Long Long Int with base-36 = " << wcstoull(str, &end, 36) << endl;
wcout << L"End String = " << end << endl << endl;
return 0;
}
運行程序時,輸出將是:
311bzϾϿ to Unsigned Long Long Int with base-5 = 81 End String = bzϾϿ 311bzϾϿ to Unsigned Long Long Int with base-12 = 5351 End String = zϾϿ 311bzϾϿ to Unsigned Long Long Int with base-36 = 5087231 End String = ϾϿ
wcstoull() 函數會忽略所有前導空白字符,直到找到主要的非空白字符。
通常,wcstoull() 函數的有效整數參數具有以下形式:
[whitespace] [- | +] [0 | 0x] [alphanumeric characters]
然後,從這個字符開始,它需要盡可能多的字符來形成一個有效的整數表示,並將它們轉換為一個 unsigned long int 值。在最後一個有效字符之後字符串的剩餘部分將被忽略並且對結果沒有影響。
示例 3:wcstoull() 函數用於前導空格和無效轉換
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t *end;
wcout << L" 205\u03e2x to Unsigned Long Long Int with base-5 = " << wcstoull(L" 205\u03e2x", &end, 5) << endl;
wcout << L"End String = " << end << endl << endl;
wcout << L"x\u019cz201 to Unsigned Long Long Int with base-12 = " << wcstoull(L"x\u019cz201", &end, 12) << endl;
wcout << L"End String = " << end << endl << endl;
return 0;
}
運行程序時,輸出將是:
205Ϣx to Unsigned Long Long Int with base-5 = 10 End String = 5Ϣx xƜz201 to Unsigned Long Long Int with base-12 = 0 End String = xƜz201
如果 base
為 0,則通過查看字符串的格式自動確定數字基數。如果前綴為 0,則 base
為八進製 (8)。如果前綴為 0x 或 0X,則 base
為十六進製 (16),否則 base
為十進製 (10)。
示例 4:wcstoull() 以 0 為基數的函數
#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t *end;
wcout << L"0539\u1e84 to Unsigned Long Long Int with base-0 = " << wcstoull(L"0539\u1e84", &end, 0) << endl;
wcout << L"End String = " << end << endl << endl;
wcout << L"0xa31\u05e2 to Unsigned Long Long Int with base-0 = " << wcstoull(L"0xa31\u05e2", &end, 0) << endl;
wcout << L"End String = " << end << endl << endl;
wcout << L"119x\u060f to Unsigned Long Long Int with base-0 = " << wcstoull(L"119x\u060f", &end, 0) << endl;
wcout << L"End String = " << end << endl << endl;
return 0;
}
運行程序時,輸出將是:
0539Ẅ to Unsigned Long Long Int with base-0 = 43 End String = 9Ẅ 0xa31ע to Unsigned Long Long Int with base-0 = 2609 End String = ע 119x؏ to Unsigned Long Long Int with base-0 = 119 End String = x؏
相關用法
- C++ wcstoul()用法及代碼示例
- C++ wcstold()用法及代碼示例
- C++ wcstod()用法及代碼示例
- C++ wcstok()用法及代碼示例
- C++ wcstof()用法及代碼示例
- C++ wcstol()用法及代碼示例
- C++ wcstombs()用法及代碼示例
- C++ wcstoll()用法及代碼示例
- C++ wcsftime()用法及代碼示例
- C++ wcscspn()用法及代碼示例
- C++ wcsncmp()用法及代碼示例
- C++ wcsstr()用法及代碼示例
- C++ wcsrchr()用法及代碼示例
- C++ wcsncpy()用法及代碼示例
- C++ wcslen()用法及代碼示例
- C++ wcsspn()用法及代碼示例
- C++ wcschr()用法及代碼示例
- C++ wcspbrk()用法及代碼示例
- C++ wcscat()用法及代碼示例
- C++ wcscoll()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ wcstoull()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。