📅  最后修改于: 2020-09-25 10:10:17             🧑  作者: Mango
该wcstoul() 函数还设置一个指针指向第一个字符 ,如果有任何的宽字符串的最后一个有效字符之后,否则指针设置为null。
For base 10 and the wide string L"29hi$"
Valid numeric part -> 29
First character after valid numeric part -> h
它在
unsigned long wcstoul( const wchar_t* str, wchar_t** str_end, int base );
所述wcstoul() 函数获得一个宽字符串 str
,一个指针到宽字符 str_end
和一个整数值- base
作为其参数。
然后,它将宽字符串的内容解释为给定base
的无符号整数,并返回无符号long int值。
wcstoul() 函数返回:
#include
#include
#include
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 value;
int base = 10;
value = wcstoul(str1, &end, base);
wcout << L"String value = " << str1 << endl;
wcout << L"Unsigned Long Int value = " << value << endl;
wcout << L"End String = " << end << endl;
value = wcstoul(str2, &end, base);
wcout << L"String value = " << str2 << endl;
wcout << L"Unsigned Long Int value = " << value << endl;
wcout << L"End String = " << end << endl;
return 0;
}
运行该程序时,输出为:
String value = 101aaᚶ
Unsigned Long Int value = 101
End String = aaᚶ
String value = 59
Unsigned Long Int value = 59
End String =
wcstoul() 函数的有效整数值包括:
如果参数的开头包含减号(-),则负数将隐式转换为无符号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},依此类推。
#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 Unsigned Long Int with base-5 = " << wcstoul(str, &end, 5) << endl;
wcout << L"End String = " << end << endl << endl;
wcout << str << L" to Unsigned Long Int with base-12 = " << wcstoul(str, &end, 12) << endl;
wcout << L"End String = " << end << endl << endl;
wcout << str << L" to Unsigned Long Int with base-36 = " << wcstoul(str, &end, 36) << endl;
wcout << L"End String = " << end << endl << endl;
return 0;
}
运行该程序时,输出为:
311bzϾϿ to Unsigned Long Int with base-5 = 81
End String = bzϾϿ
311bzϾϿ to Unsigned Long Int with base-12 = 5351
End String = zϾϿ
311bzϾϿ to Unsigned Long Int with base-36 = 5087231
End String = ϾϿ
直到主非空白字符被找到wcstoul() 函数将忽略所有的前导空白字符 。
通常,wcstoul() 函数的有效整数参数具有以下形式:
[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 Unsigned Long Int with base-5 = " << wcstoul(L" 205\u03e2x", &end, 5) << endl;
wcout << L"End String = " << end << endl << endl;
wcout << L"x\u019cz201 to Unsigned Long Int with base-12 = " << wcstoul(L"x\u019cz201", &end, 12) << endl;
wcout << L"End String = " << end << endl << endl;
return 0;
}
运行该程序时,输出为:
205Ϣx to Unsigned Long Int with base-5 = 10
End String = 5Ϣx
xƜz201 to Unsigned Long Int with base-12 = 0
End String = xƜz201
如果base
为0,则通过查看字符串的格式自动确定数字基数。如果前缀为0,则base
为八进制(8)。如果前缀为0x或0X,则base
为十六进制(16),否则基数为十进制(10)。
#include
#include
#include
using namespace std;
int main()
{
setlocale(LC_ALL, "en_US.UTF-8");
wchar_t *end;
wcout << L"0539\u1e84 to Unsigned Long Int with base-0 = " << wcstoul(L"0539\u1e84", &end, 0) << endl;
wcout << L"End String = " << end << endl << endl;
wcout << L"0xa31\u05e2 to Unsigned Long Int with base-0 = " << wcstoul(L"0xa31\u05e2", &end, 0) << endl;
wcout << L"End String = " << end << endl << endl;
wcout << L"119x\u060f to Unsigned Long Int with base-0 = " << wcstoul(L"119x\u060f", &end, 0) << endl;
wcout << L"End String = " << end << endl << endl;
return 0;
}
运行该程序时,输出为:
0539Ẅ to Unsigned Long Int with base-0 = 43
End String = 9Ẅ
0xa31ע to Unsigned Long Int with base-0 = 2609
End String = ע
119x؏ to Unsigned Long Int with base-0 = 119
End String = x؏