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