📜  C++ wcstol()

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

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

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

它在头文件中定义。

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值。

wcstol()参数

wcstol()返回值

wcstol() 函数返回:

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

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

参数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 
#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 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 
#include 
#include 
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:以0为底的wcstol() 函数

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