c++中将字符串转换为无符号整数函数:std::stoul and std::stoull

本文详细介绍了C++标准库中用于将字符串转换为无符号整数的函数std::stoul和std::stoull,包括参数说明和实例演示。通过示例代码展示了如何将十六进制字符串转换为十进制数值,并进行数值比较。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门 ==>> AutoSAR实战系列300讲总目录

1 std::stoul

将字符串转换为无符号整数。解析 str ,将其内容解释为指定基数的整数,该基数作为无符号长整型值返回。

unsigned long stoul (const string&  str, size_t* idx = 0, int base = 10);
Parameters :
str : String object with the  representation of an integral number.

idx : Pointer to an object of type size_t, whose value is set by the function to position of the next character in str after the numerical value.
This parameter can also be a null pointer, in which case
it is not used.

base : Numerical base (radix) that determines the valid characters and their interpretation.
If this is 0, the base used is determined by the format in
the sequence.Notice that by default this argument is 10, not 0. 

2 std::stoull

将字符串转换为 unsigned long long。解析 str 将其内容解释为指定基数的整数,该基数作为 unsigned long long 类型的值返回。

unsigned long long stoull 
(const string&  str, size_t* idx = 0, int base = 10);
Parameters :
str : String object with the representation of an integral number.

idx : Pointer to an object of type size_t, whose value is set by the function to position of the next  character in str after the numerical value. This parameter can  also be a null pointer, in which case it is not used.

base : Numerical base (radix) that determines  the valid characters and their interpretation.
If this is 0, the base used is determined by the format in the
sequence. Notice that by default this argument is 10, not 0. 

3 实例解析

Input : FF
Output : 255

Input : FFFFF
Output :16777215 
// CPP code to convert hexadecimal
// string to int
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Hexadecimal string
    string str = "FF";
  
    // Converted integer
    unsigned long num = stoul(str, nullptr, 16);
  
    // Printing the integer
    cout << num << "\n";
  
    // Hexadecimal string
    string st = "FFFFFF";
  
    // Converted long long
    unsigned long long val = stoull(st, nullptr, 16);
  
    // Printing the long long
    cout << val;
    return 0;
}

另一个例子:比较两个包含十六进制值的字符串的程序这里使用stoul,但在数字超过 unsigned long 值的情况下,使用stoull。

// CPP code to compare two
// hexadecimal string
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Hexadecimal string
    string s1 = "4F";
    string s2 = "A0";
  
    // Converted integer
    unsigned long n1 = stoul(s1, nullptr, 16);
    unsigned long n2 = stoul(s2, nullptr, 16);
  
    // Compare both string
    if (n1 > n2)
        cout << s1 << " is greater than " << s2;
    else if (n2 > n1)
        cout << s2 << " is greater than " << s1;
    else
        cout << "Both " << s1 << " and " << s2 << " are equal";
    return 0;
}
输出:

A0 is greater than 4F
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果Autosar

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值