当前位置: 首页 > news >正文

C++ - 数据处理之数值转不同进制的字符串(数值转十进制字符串、数值转八进制字符串、数值转二进制字符串、数值转十六进制字符串)

一、数值转十进制字符串

  1. 使用 to_string 方法
#include <iostream>
#include <string>using namespace std;int main() {int num = 42;string decStr = to_string(num);cout << decStr << endl;return 0;
}
# 输出结果42
  1. 使用字符串流
#include <iostream>
#include <string>
#include <sstream>using namespace std;int main() {int num = 42;ostringstream oss;oss << num;string decStr = to_string(num);cout << decStr << endl;return 0;
}
# 输出结果42

二、数值转八进制字符串

  • 使用字符串流
#include <iostream>
#include <string>
#include <sstream>using namespace std;int main() {int num = 42;ostringstream oss;oss << oct << num;string octStr = oss.str();cout << octStr << endl;return 0;
}
# 输出结果52

三、数值转二进制字符串

1、转换并补零
  • 使用 bitset 方法
#include <iostream>
#include <string>
#include <bitset>using namespace std;int main() {int num = 42;string binStr = bitset<8>(num).to_string();cout << binStr << endl;return 0;
}
# 输出结果00101010
2、转换不补零
  1. 使用 find_first_not_of 方法跳过前导 0
#include <iostream>
#include <string>
#include <bitset>using namespace std;int main() {int num = 42;string binStr = bitset<8>(num).to_string();// 找到第一个不是 0 的位置size_t firstNonZero = binStr.find_first_not_of('0');// 如果全是零(num 为 0),则保留if (firstNonZero == string::npos) {firstNonZero = binStr.size() - 1;}// 截取从第一个非零到末尾的子串binStr = binStr.substr(firstNonZero);cout << binStr << endl;return 0;
}
# 输出结果101010
  1. 手动计算二进制
#include <iostream>
#include <string>
#include <algorithm>using namespace std;string intToBinary(int num) {if (num == 0) return "0";string binStr;while (num > 0) {binStr += (num % 2) ? '1' : '0';num /= 2;}reverse(binStr.begin(), binStr.end()); // 反转得到正确顺序return binStr;
}int main() {int num = 42;string binStr = intToBinary(num);cout << binStr << endl;return 0;
}
# 输出结果101010

四、数值转十六进制字符串

1、基本转换
  • 使用字符串流
#include <iostream>
#include <string>
#include <sstream>using namespace std;int main() {int num = 42;ostringstream oss;// 小写oss << hex << num;string hexStr1 = oss.str();oss.str("");// 大写oss << uppercase << hex << num;string hexStr2 = oss.str();cout << hexStr1 << endl;cout << hexStr2 << endl;return 0;
}
# 输出结果2a
2A
2、补零
  • 使用字符串流 + setw 方法 + setfill 方法
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>using namespace std;int main() {int num = 42;ostringstream oss;// 小写oss << hex << setw(4) << setfill('0') << num;string hexStr1 = oss.str();oss.str("");// 大写oss << uppercase << hex << setw(4) << setfill('0') << num;string hexStr2 = oss.str();cout << hexStr1 << endl;cout << hexStr2 << endl;return 0;
}
# 输出结果002a
002A
http://www.xdnf.cn/news/766009.html

相关文章:

  • 黑马程序员C++核心编程笔记--4 类和对象--多态
  • 《信号与系统》--期末总结V1.0
  • linux 的devmem2 调式使用说明
  • Vue-3-前端框架Vue基础入门之VSCode开发环境配置和Tomcat部署Vue项目
  • 常见ADB指令
  • Vue-4-前端框架Vue基础入门之Vue的常用操作
  • opencv调用模型
  • 渗透实战PortSwigger Labs AngularJS DOM XSS利用详解
  • 【MySQL】视图与用户管理
  • linux——文件系统
  • 使用API网关Kong配置反向代理和负载均衡
  • IoTGateway项目生成Api并通过swagger和Postman调用
  • Fisher准则例题——给定类内散度矩阵和类样本均值
  • 数据库系统概论(十六)数据库安全性(安全标准,控制,视图机制,审计与数据加密)
  • 好用的C/C++/嵌入式 IDE: CLion的下载安装教程(保姆级教程)
  • 专注成就技术传奇:一路向前的力量
  • 设备驱动与文件系统:03 生磁盘的使用
  • Android高级开发第三篇 - JNI异常处理与线程安全编程
  • HarmonyOS鸿蒙Taro跨端框架
  • STM32CubeDAC及DMA配置
  • 高效微调方法简述
  • 网络地址转换
  • Python趣学篇:用Pygame打造绚烂流星雨动画
  • Nacos 2.4.3 登录配置
  • 云计算数据治理
  • 大模型的开发应用(六):使用 Xtuner QLoRA 微调模型
  • 使用 PHP 和 Guzzle 对接印度股票数据源API
  • Java 2D 图形类总结与分类
  • Node.js 中使用 Express 框架系统详细讲解
  • 3516cv610在sample_aiisp上多创一路编码流,方法