C++中进制转换问题

一直在刷题的时候,都会遇到一个坑,就是进制转换的问题。而每一次都傻乎乎的自己去实现一个。所以算是对以前的坑的一个总结。

  • itoa 函数

    • itoa是广泛应用的非标准C语言和C++语言扩展函数。由于它不是标准C/C++语言函数,所以不能在所有的编译器中使用。但是,大多数的编译器(如Windows上的)通常在<stdlib.h>/<cstdlib>头文件中包含这个函数。
      //函数原型
      char *itoa(int value, char *str, int radix)
      
      value 是被转换的整数
      str 转换后存储的字符数组
      radix 转换进制数,可以是 2, 8, 10, 16 等等
      
      由上可知,itoa可以实现10到2、8、16的转换,8到2、10、16的转换,16到2、8、10的转换,唯独没有2进制到其他进制的转换
    • 相关测试如下:
      #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
      ", str);
      
          memset(str, 0, sizeof(str));
          itoa(98, str, 8);
          printf("10--8进制: %s
      ", str);
      
          memset(str, 0, sizeof(str));
          itoa(98, str, 16);
          printf("10--16进制: %s
      ", str);
      
          memset(str, 0, sizeof(str));
          itoa(076, str, 2);
          printf("8--2进制: %s
      ", str);
      
          memset(str, 0, sizeof(str));
          itoa(076, str, 10);
          printf("8--10进制: %s
      ", str);
      
          memset(str, 0, sizeof(str));
          itoa(076, str, 16);
      	printf("8--16进制: %s
      ", str);
      
          memset(str, 0, sizeof(str));
          itoa(0xffff, str, 2);
          printf("16--2进制: %s
      ", str);
      
          memset(str, 0, sizeof(str));
          itoa(0xffff, str, 8);
          printf("16--8进制: %s
      ", str);
      
          memset(str, 0, sizeof(str));
          itoa(0xffff, str, 10);
          printf("16--10进制: %s
      ", str);
          return 0;
      }
      
      程序运行结果如下:
  • sprintf 函数

    • 把格式化数据写入某个字符缓冲区中,头文件为<stdio.h>/<cstdio>
      //函数原型
      int sprintf(char *buffer, const char *format, [argument]...)
      
      buffer: char*型数组,指向将要写入的字符串的缓冲区
      format: 格式化字符串
      [argument]...: 可选参数,就是任何类型的数据
      
      sprintf可完成8,10,16进制间的互相转换。
    • 相关测试如下:
      #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
      ", str);
      
          memset(str, 0, sizeof(str));
          sprintf(str, "%x", 1024);
          printf("16进制: %s
      ", 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) << "]
      ";
          std::cout << str_hex << ": " << i_hex << '
      ';
          std::cout << str_bin << ": " << i_bin << '
      ';
          std::cout << str_auto << ": " << i_auto << '
      ';
      }
      

      结果如下:

    • 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.
      ";
          return 0;
      }
      

      结果如下:

    • 其他函数可以根据这两个类推出来,不再赘述。

  • 总结:

    • stoi,stod等函数为C++11新加入,应该掌握
    • sprintf 功能略强大,值得研究注意
    • itoa 非标准库函数
    • 下一次,我不希望看到你再写跟进制转换相关的东西了,有现成的就该用。
原文地址:https://www.cnblogs.com/lengender-12/p/6667934.html