文章目录

  • 1 std::stoul
  • 2 std::stoull
  • 3 实例解析

传送门 ==>> 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 : 255Input : FFFFF
Output :16777215
// CPP code to convert hexadecimal
// string to int
#include <bits/stdc++.h>
using namespace std;int main()
{// Hexadecimal stringstring str = "FF";// Converted integerunsigned long num = stoul(str, nullptr, 16);// Printing the integercout << num << "\n";// Hexadecimal stringstring st = "FFFFFF";// Converted long longunsigned long long val = stoull(st, nullptr, 16);// Printing the long longcout << val;return 0;
}

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

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

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

  1. 在C ++中将字符串转换为int

    In this article, we will look at how we can convert a string to int in C++. Often, we may need to co ...

  2. php 字符串放到数组中,在PHP中将字符串转换为数组(Converting string into array in php)...

    在PHP中将字符串转换为数组(Converting string into array in php) 我有像下面的字符串 ["Day1"]["Morning" ...

  3. C++中将字符串转换为数字

    C++中将字符串转换为数字 法一: int t = s[len - 1]-'0';//减去一个 '0' 是 将最后一位字母转换成数字 例如:这个判断基偶性就是为了防止越界所以将数组转换为数组传入,再将 ...

  4. 在javascript中将字符串转换为数字的6种方法

    在javascript中,数字可以用两种不同的方式表示, 1.作为实际数字. 2. 作为字符串 . 很多时候,我们需要在javascript中将字符串转换为数字. 我们将看到6种不同的方法可以将字符串 ...

  5. android 字符串 转公式,java – 在android中将字符串转换为bigdecimal

    嗨我怎么能在android中将字符串转换为bigdecimal. 这是我的第一项活动: public class ViewCartActivity extends Activity { String ...

  6. javascript中将字符串转换为json格式的三种方法

    摘自:http://www.phpzixue.cn/detail1128.shtml javascript中将字符串转换为json格式的三种方法:  json在我们js的开发过程中经常会用到像在使用a ...

  7. 在Java中将字符串转换为日期,将日期转换为字符串

    Sometimes we have to Convert String to Date in java program or convert Date to String in a different ...

  8. Oracle中将字符串转换为数字、to_number()函数的使用方法

    Oracle将字符串转换为数字 将char或者varchar2类型的String转换为数值类型的格式,需要注意的是,被转换的字符串必须符合数值类型格式,否则报错. 1.to_number()函数的使用 ...

  9. 如何在JavaScript中将字符串转换为布尔值?

    我可以将表示布尔值(例如" true"," false")的字符串转换为JavaScript中的固有类型吗? 我有一个隐藏的HTML表单,该表单会根据用户在列表 ...

最新文章

  1. 关闭笔记本显示器指定组合键才能打开_笔记本外接显示器怎么设置 笔记本外接显示器设置方法【详解】...
  2. 为什么Firefox 3及之后的版本不能加载本地的JavaScript文件了?
  3. Python+sklearn使用线性回归算法预测儿童身高
  4. Ren获得Zcash技术咨询委员会资助,将在币安智能链上启动RenZEC流动性引导计划
  5. 装机 win7 64 IE11
  6. 【冷笑话】看谁跑的快?
  7. MyBatis配置使用
  8. 【codeBase_C++】C++ 读取npy文件
  9. polyval polyvalm
  10. uniapp中text-indent不起作用,uniapp首行缩进不管用如何解决?
  11. 让vscode完美支持go vendor的代码跳转(使用vscode必看)
  12. 我也来说说“自学IT能走多远”
  13. Vue前端开发文档(完善中)
  14. 讯飞智能录音笔SR502内存升级为32G,帮用户留住更多动听旋律
  15. 去掉字符串中的所有空格
  16. PyTorch学习系列教程:构建一个深度学习模型需要哪几步?
  17. my97 datepicker 自定义事件
  18. springboot排错
  19. 处理医疗影像的Python利器:PyDicom
  20. 硬核追星!杨超越杯决赛是些啥编程项目?

热门文章

  1. 2022大健康展,2022山东健康产业展,艾灸设备展9月举办
  2. 小数点怎么进行进制转换?
  3. 新的 ES2022 规范终于发布了,我总结了8个实用的新功能
  4. ifix从sqlserver里读数据_基于GE Fanuc产品PBS汽车总装生产线监控系统设计
  5. Unity录屏的坑(FFmpeg)
  6. 数字藏品在国内现状是怎样的?
  7. 从JDK8到JDK17,需要注意的一些新特性
  8. 微信小程序与普通网页开发的区别
  9. js逆向验证码篇之极验4代
  10. 别人的六一兴高彩烈,我的六一苦逼的敲代码采集壁纸~