C++中的进制转换难题
最编程
2024-01-12 22:02:36
...
itoa 函数
- itoa是广泛应用的非标准C语言和C++语言扩展函数。由于它不是标准C/C++语言函数,所以不能在所有的编译器中使用。但是,大多数的编译器(如Windows上的)通常在<stdlib.h>/<cstdlib>头文件中包含这个函数。
由上可知,itoa可以实现10到2、8、16的转换,8到2、10、16的转换,16到2、8、10的转换,唯独没有2进制到其他进制的转换//函数原型 char *itoa(int value, char *str, int radix) value 是被转换的整数 str 转换后存储的字符数组 radix 转换进制数,可以是 2, 8, 10, 16 等等
- 相关测试如下:
程序运行结果如下:#include<stdio.h> #include<stdlib.h> #include<string.h> int main() { char str[25]; memset(str, 0, sizeof(str)); itoa(98, str, 2); printf("10--2进制: %s\n", str); memset(str, 0, sizeof(str)); itoa(98, str, 8); printf("10--8进制: %s\n", str); memset(str, 0, sizeof(str)); itoa(98, str, 16); printf("10--16进制: %s\n", str); memset(str, 0, sizeof(str)); itoa(076, str, 2); printf("8--2进制: %s\n", str); memset(str, 0, sizeof(str)); itoa(076, str, 10); printf("8--10进制: %s\n", str); memset(str, 0, sizeof(str)); itoa(076, str, 16); printf("8--16进制: %s\n", str); memset(str, 0, sizeof(str)); itoa(0xffff, str, 2); printf("16--2进制: %s\n", str); memset(str, 0, sizeof(str)); itoa(0xffff, str, 8); printf("16--8进制: %s\n", str); memset(str, 0, sizeof(str)); itoa(0xffff, str, 10); printf("16--10进制: %s\n", str); return 0; }
sprintf 函数
- 把格式化数据写入某个字符缓冲区中,头文件为<stdio.h>/<cstdio>
sprintf可完成8,10,16进制间的互相转换。//函数原型 int sprintf(char *buffer, const char *format, [argument]...) buffer: char*型数组,指向将要写入的字符串的缓冲区 format: 格式化字符串 [argument]...: 可选参数,就是任何类型的数据
- 相关测试如下:
程序结果如下:#include<stdio.h> #include<string.h> #include<stdlib.h> int main(){ char str[25]; memset(str, 0, sizeof(str)); sprintf(str, "%o", 1024); printf("8进制: %s\n", str); memset(str, 0, sizeof(str)); sprintf(str, "%x", 1024); printf("16进制: %s\n", str); return 0; }
stoi, stol, stoll, stof, stod, stold
-
C++11新引入的string的api, 完成string向数值类型的转换
-
stoi:可实现任意进制数转10进制数,返回一个int数
//函数原型 int stoi(const string &str, size_t* idx = 0, int base = 10) str: 要转换的string idx: 为size_t*类型,是从str中解析出一个整数后的下一个字符的位置 base: 指出string中要转换的数的进制,即str所代表的是个什么进制的数,如是base默认为10, 若base = 0, 表示由编译器自动判定str所代表的数的进制
测试如下:
#include<iostream> #include<string> using namespace std; int main(){ string str_dec = "2048,hello world"; string str_hex = "40c3"; string str_bin = "-10010110001"; string str_auto = "0x7f"; size_t sz; // alias of size_t int i_dec = std::stoi(str_dec, &sz); int i_hex = std::stoi(str_hex, nullptr, 16); int i_bin = std::stoi(str_bin, nullptr, 2); int i_auto = std::stoi(str_auto, nullptr, 0); std::cout << str_dec << ": " << i_dec << " and [" << str_dec.substr(sz) << "]\n"; std::cout << str_hex << ": " << i_hex << '\n'; std::cout << str_bin << ": " << i_bin << '\n'; std::cout << str_auto << ": " << i_auto << '\n'; }
结果如下:
-
stod:将str转为double
//函数原型 double stod(const string &str, size_t *idx = 0) str: 要转换的string idex: 保存转换后的下一个字符位置
测试如下:
#include<iostream> #include<string> using namespace std; int main(){ std::string orbits ("365.24 29.53"); std::string::size_type sz; // alias of size_t double earth = std::stod (orbits,&sz); double moon = std::stod (orbits.substr(sz)); std::cout << "The moon completes " << (earth/moon) << " orbits per Earth year.\n"; return 0; }
结果如下:
-
其他函数可以根据这两个类推出来,不再赘述。
总结:
- stoi,stod等函数为C++11新加入,应该掌握
- sprintf 功能略强大,值得研究注意
- itoa 非标准库函数
- 下一次,我不希望看到你再写跟进制转换相关的东西了,有现成的就该用。
推荐阅读
-
一起了解 C++ 中的删除和擦除!
-
实践中的软件测试技术:设计、工具和管理 - 第 2 章第 2.3 节 使用状态转换图设计测试用例
-
C++ 获取字符串中的 ID 编号
-
整数(int)和十六进制字符(hex)的相互转换
-
java 实现十六进制数与图片的相互转换
-
Matplotlib 中几种默认颜色的十六进制表示和预览
-
十六进制和 RGB 颜色值相互转换的原理
-
照片特定风格转换 Stylar AI;GPT-4V 开放源替代 InternVL;纯 C/C++ 实现的稳定扩散库;基于 AI 的数据爬行
-
C++ 中的类和对象 (I)
-
位、字节、WORD、DWORD 的区别和联系 - Unicode 和 ANSI 的区别就像输入法中 "全宽 "和 "半宽 "的区别一样。 由于不同的 ANSI 编码有不同的标准(不同的字符集),对于给定的多字节字符串,我们必须知道它使用的是哪种字符集,才能知道它包含哪些 "字符"。对于 UNICODE 字符串来说,无论环境如何,它所代表的 "字符 "内容始终是相同的。Unicode 有一个统一的标准,定义了世界上大多数字符的编码,因此拉丁文、数字、简体中文、繁体中文和日文都可以存储在一个编码中。统一码是一个统一的标准,定义了世界上大多数字符的编码。 比特(Bit)和字节(Byte)的区别:例如USB2.0 标准接口的传输速率为 480Mbps,有一些人误认为是每秒 480 兆比特,同样网络带宽为 2MB,就容易误认为是每秒 2 兆比特。其实,480Mbps 应该是 480 兆比特/秒或 480 兆字节/秒,它等于 "60 兆字节/秒";同样,2MB,应该是 256 兆字节/秒。 Bit 和 Byte 译为 "比特",都是数据计量单位,比特="位 "或 "比特"。 Byte = 字节,即 1byte = 8bits,两者的换算关系为 1:8。 Mbps = mega bits per second(兆位/秒)是速率单位,因此 2M 带宽应为 2 兆位/秒,即 2MBps。MB = 兆字节(Megabytes,兆字节)是单位量,1MB/S(兆字节/秒)= 8MBPS(兆字节/秒)。 通常所说的硬盘容量是指 40GB、80GB、100GB,其中的 B 是指 Byte 也称为 "字节"。 1 KB=1024 字节 1 MB=1024 KB=1024*1024 字节 1 GB=1024 MB=1024*1024*1024 字节 例如,以前所谓的 56KB MODEM 转换过来的 56KBps 除以 8 就是 7Kbyte,所以真正从网上下载文件存在硬盘上的速度也是每秒 7Kbyte;也就是说,用 B 表示传输速度一般指 Bit;用 B 表示容量一般指 Byte。比特、字节、WORD、DWORD 的本质。