C++stoul、stoull 函数用法

stoul() 函数

#include <string>

unsigned long stoul(const std::string& str, std::size_t* pos = 0, int base = 10);

unsigned long stoul(const std::wstring& str, std::size_t* pos = 0, int base = 10);

功能:将字符串 str 转成 unsigned long 整数
参数:
str:字符串
pos:存储将字符串 str 转成 unsigned long 整数,处理了 str 中字符的个数的地址,默认为 NULL
base:进制,10:十进制,8:八进制,16:十六进制,0:则自动检测数值进制,str 是 0 开头为八进制,str 是 0x 或 0X 开头是十六进制,默认为十进制

stoul() 函数指定转换字符串为十进制用法

stoul.cpp
#include <iostream>
#include <string>
#include <limits.h>
using namespace std;

int main(int argc, char *argv[])
{
    unsigned long a; //x86_64构架下,unsigned long 8个字节
    size_t pos = 0;
    string str;

    cout << "ULONG_MAX = " << ULONG_MAX << endl;

    str = "-1"; 
    a = stoul(str); //数据在内存中是以补码的形式存储的,负数的补码等于反码加1
    /*
    -1的原码是:10000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
    -1的反码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111110
    -1的补码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111
    */
    cout << "a = " << a << endl; // a = (最大值) +(负数)+ 1,即:((2^64) - 1) + (-1) + 1 

    str = "1235";
    a = stoul(str);
    cout << "a = " << a << endl; //a = 1235

    str = "  -12  35"; 
    a = stoul(str, &pos); //会舍弃空白符
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-12) + 1
    cout << "pos = " << pos << endl; //pos = 5

    str = "  -12ab35";
    a = stoul(str, &pos);
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-12) + 1
    cout << "pos = " << pos << endl; //pos = 5

    str = "0123";
    a = stoul(str);
    cout << "a = " << a << endl; //a = 123

    str = "0x123";
    a = stoul(str);
    cout << "a = " << a << endl; //a = 0

    return 0;
}

stoul() 函数指定转换字符串为十六进制用法

stoul.cpp
#include <iostream>
#include <string>
#include <limits.h>
using namespace std;

int main(int argc, char *argv[])
{
    unsigned long a; //x86_64构架下,unsigned long 8个字节
    size_t pos = 0;
    string str;

    cout << "ULONG_MAX = " << ULONG_MAX << endl;

    /*
    数据在内存中是以补码的形式存储的,负数的补码等于反码加1
    -1的原码是:10000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
    -1的反码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111110
    -1的补码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111
    -1转成unsigned long:(最大值) +(负数)+ 1
    -1转成unsigned long:((2^64) - 1) + (-1) + 1
    */
    str = "0x123"; 
    a = stoul(str, NULL, 16); //base = 16,指定十六进制
    cout << "a = " << a << endl; //a = 3 + 2*16 + 1*16*16 = 291

    str = "0x123";
    a = stoul(str, NULL, 0); //base = 0,自动检测数值进制
    cout << "a = " << a << endl; //a = 291

    str = "  -12"; 
    a = stoul(str, &pos, 16); //会舍弃空白符
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-(2 + 1*16)) + 1
    cout << "pos = " << pos << endl; //pos = 5

    str = "  -12 35";
    a = stoul(str, &pos, 16);
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-(2 + 1*16)) + 1
    cout << "pos = " << pos << endl; //pos = 5

    str = "  -ab";
    a = stoul(str, NULL, 16);
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-(11 + 10*16)) + 1

    str = "0123";
    a = stoul(str, NULL, 16);   
    cout << "a = " << a << endl; //a = (3 + 2*16 + 1*16*16) = 291

    return 0;
}

stoul() 函数指定转换字符串为八进制用法

stoul.cpp
#include <iostream>
#include <string>
#include <limits.h>
using namespace std;

int main(int argc, char *argv[])
{
    unsigned long a; //x86_64构架下,unsigned long 8个字节
    size_t pos = 0;
    string str;

    cout << "ULONG_MAX = " << ULONG_MAX << endl;

    /*
    数据在内存中是以补码的形式存储的,负数的补码等于反码加1
    -1的原码是:10000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
    -1的反码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111110
    -1的补码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111
    -1转成unsigned long:(最大值) +(负数)+ 1
    -1转成unsigned long:((2^64) - 1) + (-1) + 1
    */
    str = "0x123";
    a = stoul(str, NULL, 8); //base = 8,指定八进制
    cout << "a = " << a << endl; //a = 0

   str = "0123"; //(3 + 2*8 + 1*8*8)
    a = stoul(str, NULL, 0); //base = 0,自动检测数值进制
    cout << "a = " << a << endl; //a = (3 + 2*8 + 1*8*8) = 83

    str = "-12";
    a = stoul(str, &pos, 8); //-(2 + 1*8)
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-10) + 1
    cout << "pos = " << pos << endl; //pos = 3

    str = "12";
    a = stoul(str, &pos, 8); //2 + 1*8
    cout << "a = " << a << endl; //a = 10
    cout << "pos = " << pos << endl; //pos = 2

    str = "  -12  35"; 
    a = stoul(str, &pos, 8); //会舍弃空白符
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-10) + 1
    cout << "pos = " << pos << endl; //pos = 5

    // str = "  -a78"; 
    // a = stol(str, &pos, 8); //数字前有字母,调用会崩掉
    // cout << "a = " << a << endl; 
    // cout << "pos = " << pos << endl; 

    return 0;
}

stoull() 函数

#include <string>

unsigned long long stoull(const std::string& str, std::size_t* pos = 0, int base = 10);

unsigned long long stoull(const std::wstring& str, std::size_t* pos = 0, int base = 10);

功能:将字符串 st r转成 unsigned long long 整数
参数:
str:字符串
pos:存储将字符串 str 转成 unsigned long long 整数,处理了 str 中字符的个数的地址,默认为 NULL
base:进制,10:十进制,8:八进制,16:十六进制,0:则自动检测数值进制,str 是 0 开头为八进制,str 是 0x 或 0X 开头是十六进制,默认为十进制

stoull() 函数指定转换字符串为十进制用法

stoull.cpp
#include <iostream>
#include <string>
#include <limits.h>
using namespace std;

int main(int argc, char *argv[])
{
    unsigned long long a; //x86_64构架下,unsigned long long 8个字节
    size_t pos = 0;
    string str;

    cout << "ULLONG_MAX = " << ULLONG_MAX << endl;

    str = "-1"; 
    a = stoull(str); //数据在内存中是以补码的形式存储的,负数的补码等于反码加1
    /*
    -1的原码是:10000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
    -1的反码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111110
    -1的补码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111
    */
    cout << "a = " << a << endl; // a = (最大值) +(负数)+ 1,即:((2^64) - 1) + (-1) + 1 

    str = "1235";
    a = stoull(str);
    cout << "a = " << a << endl; //a = 1235

    str = "  -12  35"; 
    a = stoull(str, &pos); //会舍弃空白符
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-12) + 1
    cout << "pos = " << pos << endl; //pos = 5

    str = "  -12ab35";
    a = stoull(str, &pos);
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-12) + 1
    cout << "pos = " << pos << endl; //pos = 5

    str = "0123";
    a = stoull(str);
    cout << "a = " << a << endl; //a = 123

    str = "0x123";
    a = stoull(str);
    cout << "a = " << a << endl; //a = 0

    return 0;
}

stoull() 函数指定转换字符串为十六进制用法

stoull.cpp
#include <iostream>
#include <string>
#include <limits.h>
using namespace std;

int main(int argc, char *argv[])
{
    unsigned long long a; //x86_64构架下,unsigned long long 8个字节
    size_t pos = 0;
    string str;

    cout << "ULLONG_MAX = " << ULLONG_MAX << endl;

    /*
    数据在内存中是以补码的形式存储的,负数的补码等于反码加1
    -1的原码是:10000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
    -1的反码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111110
    -1的补码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111
    -1转成unsigned long long:(最大值) +(负数)+ 1
    -1转成unsigned long long:((2^64) - 1) + (-1) + 1
    */
    str = "0x123"; 
    a = stoull(str, NULL, 16); //base = 16,指定十六进制
    cout << "a = " << a << endl; //a = 3 + 2*16 + 1*16*16 = 291

    str = "0x123";
    a = stoull(str, NULL, 0); //base = 0,自动检测数值进制
    cout << "a = " << a << endl; //a = 291

    str = "  -12"; 
    a = stoull(str, &pos, 16); //会舍弃空白符
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-(2 + 1*16)) + 1
    cout << "pos = " << pos << endl; //pos = 5

    str = "  -12 35";
    a = stoull(str, &pos, 16);
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-(2 + 1*16)) + 1
    cout << "pos = " << pos << endl; //pos = 5

    str = "  -ab";
    a = stoull(str, NULL, 16);
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-(11 + 10*16)) + 1

    str = "0123";
    a = stoull(str, NULL, 16);   
    cout << "a = " << a << endl; //a = (3 + 2*16 + 1*16*16) = 291

    return 0;
}

stoull() 函数指定转换字符串为八进制用法

stoul.cpp
#include <iostream>
#include <string>
#include <limits.h>
using namespace std;

int main(int argc, char *argv[])
{
    unsigned long long a; //x86_64构架下,unsigned long long 8个字节
    size_t pos = 0;
    string str;

    cout << "ULLONG_MAX = " << ULLONG_MAX << endl;

    /*
    数据在内存中是以补码的形式存储的,负数的补码等于反码加1
    -1的原码是:10000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
    -1的反码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111110
    -1的补码是:11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111
    -1转成unsigned long long:(最大值) +(负数)+ 1
    -1转成unsigned long long:((2^64) - 1) + (-1) + 1
    */
    str = "0x123";
    a = stoul(str, NULL, 8); //base = 8,指定八进制
    cout << "a = " << a << endl; //a = 0

   str = "0123"; //(3 + 2*8 + 1*8*8)
    a = stoul(str, NULL, 0); //base = 0,自动检测数值进制
    cout << "a = " << a << endl; //a = (3 + 2*8 + 1*8*8) = 83

    str = "-12";
    a = stoul(str, &pos, 8); //-(2 + 1*8)
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-10) + 1
    cout << "pos = " << pos << endl; //pos = 3

    str = "12";
    a = stoul(str, &pos, 8); //2 + 1*8
    cout << "a = " << a << endl; //a = 10
    cout << "pos = " << pos << endl; //pos = 2

    str = "  -12  35"; 
    a = stoul(str, &pos, 8); //会舍弃空白符
    cout << "a = " << a << endl; //a = ((2^64) - 1) + (-10) + 1
    cout << "pos = " << pos << endl; //pos = 5

    // str = "  -a78"; 
    // a = stol(str, &pos, 8); //数字前有字母,调用会崩掉
    // cout << "a = " << a << endl; 
    // cout << "pos = " << pos << endl; 

    return 0;
}

总结:

stoul 函数:将字符串转成 unsigned long 整数。

stoull 函数:将字符串转成 unsigned long long 整数。

使用时需要注意的是 stoul、stoull 函数是 C++11标准加入的,用 g++ 编译器编译时需要加参数:-std=c++11

### C++ 中将字符串转换为数字的方法 在 C++ 中,有多种方法可以实现从字符串到数值类型的转换。以下是几种常用的方式: #### 使用 `std::stoi` 和其变体函数 标准库提供了几个用于将字符串转换成不同类型的整数的函数,如 `std::stoi`, `std::stol`, `std::stoul`, `std::stoll`, `std::stoull`. 这些函数能够处理十进制、八进制以及十六进制表示形式,并且支持带有前缀的形式(比如 `"0x"` 表示十六进制)。 ```cpp #include <string> #include <iostream> int main() { std::string s = "123"; int num = std::stoi(s); // 转换为 int 类型 long lnum = std::stol(s); // 转换为 long 类型 std::cout << "Integer value: " << num << "\nLong integer value: " << lnum; } ``` 这些函数会抛出异常当遇到无法解析的情况时,因此建议使用 try-catch 块来捕获可能发生的错误[^1]. #### 使用流操作符 (`>>`) 另一个常见的做法是利用输入输出流来进行转换。通过创建一个 `istringstream` 对象并将其绑定到目标字符串上,再配合提取运算符 `>>` 来完成转换过程。 ```cpp #include <sstream> #include <string> #include <iostream> int main(){ std::string strNumber = "-789"; std::stringstream ss(strNumber); double dValue; ss >> dValue; if (!ss.fail()) { std::cout << "Converted floating point number: " << dValue << '\n'; } } ``` 这种方法相对简单直观,但是需要注意的是如果字符串中含有非数字字符,则剩余部分不会被读取出来[^2]. #### 利用 `atoi()` 或者 `atof()` 对于简单的场景下可以直接调用 C 风格的标准库中的 `atoi()` 函数把 ASCII 字符串转成对应的整数值;同样地也有 `atof()` 可以用来获取浮点数的结果。不过这两个函数并不推荐广泛采用因为它们缺乏对非法输入的有效检测机制[^3]: ```c++ char* charStr = "456"; int intValue = atoi(charStr); float floatVal = atof("123.45"); ``` 以上就是一些常用的 C++ 把字符串转化为数字的方法。每种方式都有各自的优缺点,在实际开发过程中可以根据具体需求选择最合适的一种或组合起来使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值