C++中进制转换问题

一直在刷题的时候,都会遇到一个坑,就是进制转换的问题。而每一次都傻乎乎的本身去实现一个。因此算是对之前的坑的一个总结。ios

  • itoa 函数
    • itoa是普遍应用的非标准C语言和C++语言扩展函数。因为它不是标准C/C++语言函数,因此不能在全部的编译器中使用。可是,大多数的编译器(如Windows上的)一般在<stdlib.h>/<cstdlib>头文件中包含这个函数。
      ```cpp
      //函数原型
      char itoa(int value, char str, int radix)api

      value 是被转换的整数
      str 转换后存储的字符数组
      radix 转换进制数,能够是 2, 8, 10, 16 等等
      ```
      由上可知,itoa能够实现10到二、八、16的转换,8到二、十、16的转换,16到二、八、10的转换,惟独没有2进制到其余进制的转换
    • 相关测试以下:
      ```cpp
      #include<stdio.h>
      #include<stdlib.h>
      #include<string.h>数组

      int main()
      {
      char str[25];ide

      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>
      ```cpp
      //函数原型
      int sprintf(char buffer, const char format, [argument]...)spa

      buffer: char*型数组,指向将要写入的字符串的缓冲区
      format: 格式化字符串
      [argument]...: 可选参数,就是任何类型的数据
      ```
      sprintf可完成8,10,16进制间的互相转换。
    • 相关测试以下:
      ```cpp
      #include<stdio.h>
      #include<string.h>
      #include<stdlib.h>
      int main(){
      char str[25];3d

      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;

      }
      ```
      程序结果以下:
      code

  • stoi, stol, stoll, stof, stod, stold
    • C++11新引入的string的api, 完成string向数值类型的转换
      orm

    • stoi:可实现任意进制数转10进制数,返回一个int数
      ```cpp
      //函数原型
      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所表明的数的进制
      测试以下:cpp
      #include
      #include
      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
      ```cpp
      //函数原型
      double stod(const string &str, size_t *idx = 0)

      str: 要转换的string
      idex: 保存转换后的下一个字符位置
      测试以下:cpp
      #include
      #include
      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 非标准库函数
    • 下一次,我不但愿看到你再写跟进制转换相关的东西了,有现成的就该用。
相关文章
相关标签/搜索