📜  C++ wcstoull()

📅  最后修改于: 2020-09-25 10:11:05             🧑  作者: Mango

C++中的wcstoull() 函数将宽字符串的内容解释为指定基数的无符号整数,并将其值作为无符号long long int返回。

该wcstoull() 函数还设置一个指针指向第一个字符 ,如果有任何的宽字符串的最后一个有效字符之后,否则指针设置为null。

For base 10 and the wide string L"29hi$"
Valid numeric part -> 29
First character after valid numeric part -> h

它在头文件中定义。

wcstoull()原型

unsigned long wcstoull( const wchar_t* str, wchar_t** str_end, int base );

所述wcstoull() 函数获得一个宽字符串 str ,一个指针到宽字符 str_end和一个整数值- base作为其参数。

然后,它将宽字符串的内容解释为给定base的无符号整数,并返回无符号long long int值。

wcstoull()参数

wcstoull()返回值

wcstoull() 函数返回:

示例1:wcstoull() 函数如何工作?

#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 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() 函数的有效整数值包括:

如果参数的开头包含减号(-),则负数将隐式转换为无符号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 
#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 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]

然后,从该字符开始,它需要尽可能多的字符来形成有效的整数表示形式,并将其转换为无符号的long int值。最后一个有效字符之后的字符串剩余部分将被忽略,并且对结果没有任何影响。

示例3:wcstoull() 函数用于前置空格和无效转换

#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 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 
#include 
#include 
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؏