一直在刷題的時候,都會遇到一個坑,就是進制轉換的問題。而每一次都傻乎乎的自己去實現一個。所以算是對以前的坑的一個總結。
-
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; }
- itoa是廣泛應用的非標准C語言和C++語言擴展函數。由於它不是標准C/C++語言函數,所以不能在所有的編譯器中使用。但是,大多數的編譯器(如Windows上的)通常在<stdlib.h>/<cstdlib>頭文件中包含這個函數。
-
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; }
- 把格式化數據寫入某個字符緩沖區中,頭文件為<stdio.h>/<cstdio>
-
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 非標准庫函數
- 下一次,我不希望看到你再寫跟進制轉換相關的東西了,有現成的就該用。