📜  C++ strtol()

📅  最后修改于: 2020-09-25 08:59:53             🧑  作者: Mango

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

C++中的strtol() 函数将字符串的内容解释为指定基数的整数,并将其值返回为long int。此函数还将设置一个指针,以指向该字符串的最后一个有效字符之后的第一个字符 (如果存在),否则将其设置为null。

For base 10 and the string "12abc":
Valid numeric part -> 12
First character after valid numeric part -> a

strtol()原型[从C++ 11标准开始]

long int strtol(const char* str, char** end, int base);

strtol() 函数将字符串,指向字符的指针和整数值-base作为其参数,将字符串的内容解释为给定base的整数,并返回一个long int值。

此函数在头文件中定义。

strtol()参数

strtol()返回值

strtol() 函数返回:

示例1:strtol()在C++中如何工作?

#include 
#include 

using namespace std;

int main()
{
    int base = 10;
    char str[] = "27ab_1x";
    char *end; 
    long int num;
    
    num = strtol(str, &end, base);
    cout << "Number in  String = " << str << endl;
    cout << "Number in Long Int = " << num << endl;
    cout << "End String = " << end << endl << endl;
    
    // the pointer to invalid characters can be null
    strcpy(str, "27");
    cout << "Number in  String = " << str << endl;
    num = strtol(str, &end, base);
    cout << "Number in Long Int = " << num << endl;
    if (*end) {
        cout << end;
    } else {
        cout << "Null pointer";
    }
    return 0;
}

运行该程序时,输出为:

Number in  String = 27ab_1x
Number in Long Int = 27
End String = ab_1x

Number in  String = 27
Number in Long Int = 27
Null pointer

strtol() 函数的有效整数值包括:

参数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:具有不同基数的strtol() 函数

#include 
#include 
#include 
using namespace std;

int main()
{
    char *end;
    
    cout << "128bz" << " to Long Int with base-5 = " << strtol("128bxz", &end, 5) << endl;
    cout << "End String = " << end << endl << endl;
    
    cout << "128bz" << " to Long Int with base-12 = " << strtol("128bxz", &end, 12) << endl;
    cout << "End String = " << end << endl << endl;
    
    cout << "128bz" << " to Long Int with base-36 = " << strtol("128bxz", &end, 36) << endl;
    cout << "End String = " << end << endl << endl;
    
    return 0;
}

运行该程序时,输出为:

128bz to Long Int with base-5 = 7
End String = 8bxz

128bz to Long Int with base-12 = 2123
End String = xz

128bz to Long Int with base-36 = 64214135
End String =

直到主非空白字符被找到与strtol() 函数将忽略所有的前导空白字符 。

通常,strtol() 函数的有效整数参数具有以下形式:

[whitespace] [- | +] [0 | 0x] [alphanumeric characters]

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

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

#include 
#include 
using namespace std;

int main()
{
    char *end;
    
    cout << "  25axbz" << " to Long Int with base-11 = " << strtol("  25axbz", &end, 11) << endl;
    cout << "End String = " << end << endl << endl;
    
    cout << "   110bcd" << " to Long Int with base-2 = " << strtol("   110bcd", &end, 2) << endl;
    cout << "End String = " << end << endl << endl;

    cout << "ax110.97" << " to Long Int with base-10 = " << strtol("ax110.97", &end, 10) << endl;
    cout << "End String = " << end << endl << endl;

    return 0;
}

运行该程序时,输出为:

25axbz to Long Int with base-11 = 307
End String = xbz

   110bcd to Long Int with base-2 = 6
End String = bcd

ax110.97 to Long Int with base-10 = 0
End String = ax110.97

如果基数为``0'',则通过查看字符串格式自动确定数字基数。如果前缀为0,则基数为八进制(8)。如果前缀为0x或0X,则基数为十六进制(16),否则基数为十进制(10)。

示例4:以0为底的strtol() 函数

#include 
#include 

using namespace std;

int main()
{
    char *end;
    
    /* octal base */
    cout << "0128ai" << " to Long Int with base-0 = " << strtol("0128ai", &end, 0) << endl;
    cout << "End String = " << end << endl << endl;
    
    /* hexadecimal base */
    cout << "0x15axzz" << " to Long Int with base-0 = " << strtol("0x15axzz", &end, 0) << endl;
    cout << "End String = " << end << endl << endl;
    
    /* decimal base */
    cout << "23dfl" << " to Long Int with base-0 = " << strtol("23dfl", &end, 0) << endl;
    cout << "End String = " << end << endl << endl;
    
    return 0;
}

运行该程序时,输出为:

0128ai to Long Int with base-0 = 10
End String = 8ai

0x15axzz to Long Int with base-0 = 346
End String = xzz

23dfl to Long Int with base-0 = 23
End String = dfl