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 非標準庫函數
    • 下一次,我不但願看到你再寫跟進制轉換相關的東西了,有現成的就該用。
相關文章
相關標籤/搜索