该wcstoll()
函数用于将宽字符串转换为长整数。它将指针设置为指向最后一个字符之后的第一个字符。语法如下。
long long wcstoll(const wchar_t* str, wchar_t** str_end, int base)
此函数接受三个参数。这些参数如下-
str:这是宽字符串的开头。
str_end:函数将str_end设置为最后一个有效字符之后的下一个字符(如果有),否则为null。
base:指定基数。基本值可以是(0,2,3,…,35,36)
此函数返回转换后的long long整数。当字符指向NULL时,它返回0。
#include <iostream> using namespace std; main() { //定义两个宽字符串 wchar_t string1[] = L"777HelloWorld"; wchar_t string2[] = L"565Hello"; wchar_t* End; //The end pointer int base = 10; int value; value = wcstoll(string1, &End, base); wcout << "The string Value = " << string1 << "\n"; wcout << "Long Long Int value = " << value << "\n"; wcout << "End String = " << End << "\n"; //remaining string after long long integer value = wcstoll(string2, &End, base); wcout << "\nThe string Value = " << string2 << "\n"; wcout << "Long Long Int value = " << value << "\n"; wcout << "End String = " << End; //remaining string after long long integer }
输出结果
The string Value = 777HelloWorld Long Long Int value = 777 End String = HelloWorld The string Value = 565Hello Long Long Int value = 565 End String = Hello
现在让我们看一下具有不同基值的示例。这里的底数是16。通过采用给定底数的字符串,它将以十进制格式打印。
#include <iostream> using namespace std; main() { //定义两个宽字符串 wchar_t string1[] = L"5EHelloWorld"; wchar_t string2[] = L"125Hello"; wchar_t* End; //The end pointer int base = 16; int value; value = wcstoll(string1, &End, base); wcout << "The string Value = " << string1 << "\n"; wcout << "Long Long Int value = " << value << "\n"; wcout << "End String = " << End << "\n"; //remaining string after long long integer value = wcstoll(string2, &End, base); wcout << "\nThe string Value = " << string2 << "\n"; wcout << "Long Long Int value = " << value << "\n"; wcout << "End String = " << End; //remaining string after long long integer }
输出结果
The string Value = 5EHelloWorld Long Long Int value = 94 End String = HelloWorld The string Value = 125Hello Long Long Int value = 293 End String = Hello
此处的字符串包含5E,因此其值为十进制的94,第二个字符串包含125。十进制的293。