📅  最后修改于: 2020-09-25 10:09:26             🧑  作者: Mango
该wcstoll() 函数还设置一个指针指向第一个字符 ,如果有任何的宽字符串的最后一个有效字符之后,否则指针设置为null。
For base 10 and the wide string L"31ÛÕÕ"
Valid numeric part -> 31
First character after valid numeric part -> Û
它在
long long wcstoll( const wchar_t* str, wchar_t** str_end, int base );
所述wcstoll() 函数获得一个宽字符串 str
,一个指针到宽字符 str_end
和一个整数值- base
作为其参数。
然后,它将宽字符串的内容解释为给定base
的整数,并返回long long int值。
wcstoll() 函数返回:
#include
#include
#include
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t str1[] = L"41\u0166\u0124xx";
wchar_t str2[] = L"127";
wchar_t *end;
long long value;
int base = 10;
value = wcstoll(str1, &end, base);
wcout << L"String value = " << str1 << endl;
wcout << L"Long Long Int value = " << value << endl;
wcout << L"End String = " << end << endl;
value = wcstoll(str2, &end, base);
wcout << L"String value = " << str2 << endl;
wcout << L"Long Long Int value = " << value << endl;
wcout << L"End String = " << end << endl;
return 0;
}
运行该程序时,输出为:
String value = 41ŦĤxx
Long Long Int value = 41
End String = ŦĤxx
String value = 127
Long Long Int value = 127
End String =
wcstoll() 函数的有效整数值包括:
参数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},依此类推。
#include
#include
#include
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 Long Int with base-5 = " << wcstoll(str, &end, 5) << endl;
wcout << L"End String = " << end << endl << endl;
wcout << str << L" to Long Long Int with base-12 = " << wcstoll(str, &end, 12) << endl;
wcout << L"End String = " << end << endl << endl;
wcout << str << L" to Long Long Int with base-36 = " << wcstoll(str, &end, 36) << endl;
wcout << L"End String = " << end << endl << endl;
return 0;
}
运行该程序时,输出为:
311bzϾϿ to Long Long Int with base-5 = 81
End String = bzϾϿ
311bzϾϿ to Long Long Int with base-12 = 5351
End String = zϾϿ
311bzϾϿ to Long Long Int with base-36 = 5087231
End String = ϾϿ
直到主非空白字符被找到wcstoll() 函数将忽略所有的前导空白字符 。
通常,wcstoll() 函数的有效整数参数具有以下形式:
[whitespace] [- | +] [0 | 0x] [alphanumeric characters]
然后,从这个字符开始,它需要尽可能多的字符来形成有效的整数表示形式并将其转换为long int值。
最后一个有效字符之后的字符串剩余部分将被忽略,并且对结果没有任何影响。
#include
#include
#include
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t *end;
wcout << L" 205\u03e2x to Long Long Int with base-5 = " << wcstoll(L" 205\u03e2x", &end, 5) << endl;
wcout << L"End String = " << end << endl << endl;
wcout << L"x\u019cz201 to Long Long Int with base-12 = " << wcstoll(L"x\u019cz201", &end, 12) << endl;
wcout << L"End String = " << end << endl << endl;
return 0;
}
运行该程序时,输出为:
205Ϣx to Long Long Int with base-5 = 10
End String = 5Ϣx
xƜz201 to Long Long Int with base-12 = 0
End String = xƜz201
如果base
为0,则通过查看字符串的格式自动确定数字基数。如果前缀为0,则base
为八进制(8)。如果前缀为0x或0X,则base
为十六进制(16),否则base
为十进制(10)。
#include
#include
#include
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t *end;
wcout << L"0539\u1e84 to Long Long Int with base-0 = " << wcstoll(L"0539\u1e84", &end, 0) << endl;
wcout << L"End String = " << end << endl << endl;
wcout << L"0xa31\u05e2 to Long Long Int with base-0 = " << wcstoll(L"0xa31\u05e2", &end, 0) << endl;
wcout << L"End String = " << end << endl << endl;
wcout << L"119x\u060f to Long Long Int with base-0 = " << wcstoll(L"119x\u060f", &end, 0) << endl;
wcout << L"End String = " << end << endl << endl;
return 0;
}
运行该程序时,输出为:
0539Ẅ to Long Long Int with base-0 = 43
End String = 9Ẅ
0xa31ע to Long Long Int with base-0 = 2609
End String = ע
119x؏ to Long Long Int with base-0 = 119
End String = x؏