C++中的各種進制轉換函數匯總
1.在C中,按指定進制格式輸出如下:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
printf("%o\n",35); // 八進制格式輸出:%o
printf("%d\n",35); // 十進制格式輸出:%d
printf("%x",35); // 十六進格式制輸出:%x或者%X
return 0;
}
// 輸出結果:
43
35
23
2.在C++中,按指定進制格式輸出如下:
#include <iostream>
using namespace std;
int main()
{
cout<<oct<<35<<endl; // 八進制輸出格式 oct
cout<<dec<<35<<endl; // 十進制輸出格式 dec
cout<<hex<<35<<endl; // 十六進制輸出格式 hex
return 0;
}
// 輸出:
43
35
23
3.任意2-36進制數轉化為10進制數
- 自己寫一個函數
建議自己可以去敲一敲,加深記憶
#include <iostream>
using namespace std;
int turn (string a,int t)
{
int sum=0;
for(int i=a.size()-1;i>=0;i--)
{
if(a[i]>='0'&&a[i]<='9')
sum=sum*t+a[i]-'0';
else
{
sum=sum*t+a[i]-'A'+10;
}
}
return sum;
}
int main()
{
string a;
int num,t;
while(cin>>a) // 超過10進制我們以大寫字母表示
{
cin>>t; // t代表要轉換的進制數
num=turn(a,t);
cout<<num<<endl;
}
return 0;
}
2.strtol函數
long int strtol(const char nptr, char **endptr, int base)
strtol()會將nptr指向的字符串,根據參數base,按權轉化為long int, 然后返回這個值。
參數base的范圍為2~36,和0;它決定了字符串以被轉換為整數的權值。
可以被轉換的合法字符依據base而定,舉例來說,當base為2時,合法字符為‘0’,‘1’;base為8時,合法字符為‘0’,‘1’,……‘7’;base為10時,合法字符為‘0’,‘1’,……‘9’;base 為16時,合法字符為‘0’,‘1’,……‘9’,‘a’,……‘f’;base為24時,合法字符為‘0’,……‘9’,‘a’,……‘n’,base為36時,合法字符為‘0’,……‘9’,‘a’,……‘z’;等等。其中,不區分大小寫,比如,‘A’和‘a’會都會被轉化為10。
當字符合法時,‘0’,……‘9’依次被轉換為十進制的0~9,‘a’,……‘z’一次北轉換為十進制的10~35。
strtol()函數檢測到第一個非法字符時,立即停止檢測,其后的所有字符都會被當作非法字符處理。合法字符串會被轉換為long int, 作為函數的返回值。非法字符串,即從第一個非法字符的地址,被賦給endptr。**endptr是個雙重指針,即指針的指針。strtol()函數就是通過它改變*endptr的值,即把第一個非法字符的地址傳給endptr。多數情況下,endptr設置為NULL, 即不返回非法字符串。
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char r[100];
int t;
while(~scanf("%s",r))
{
cin>>t; // t代表他原來的進制數
cout<<strtol(r,NULL,t)<<endl;
memset(r,'\0',sizeof(r));
}
return 0;
}
// C++11 特性!!!
注意:
①如果base為0,且字符串不是以0x(或者0X)開頭,則按十進制進行轉化。
②如果base為0或者16,並且字符串以0x(或者0X)開頭,那么,x(或者X)被忽略,字符串按16進制轉化。
③如果base不等於0和16,並且字符串以0x(或者0X)開頭,那么x被視為非法字符。
④對於nptr指向的字符串,其開頭和結尾處的空格被忽視,字符串中間的空格被視為非法字符。
4.將10進制數轉換為任意的n進制數
-
建議自己寫一個代碼:
#include <iostream> #include <cstring> #include <stack> using namespace std; stack<char> s; void turn (int t,int tmp) { while(t!=0) { if(t%tmp<=10) s.push(t%tmp+'0'); else s.push(t%tmp-10+'A'); t/=tmp; } } int main() { int t,tmp; while(cin>>t>>tmp) { turn(t,tmp); while(!s.empty()) { cout<<s.top(); s.pop(); } cout<<endl; } return 0; }
-
itoa函數
函數原型:*char*
*itoa(``int
value,``char``*string,``int
radix);
例如:itoa(num, str, 2); num是一個int型的,是要轉化的10進制數,str是轉化結果,后面的值為目標進制。
PS:
itoa並不是一個標准的函數,而是一個windows所特有的,如需要跨平台請使用sprintf
#include<cstdio> #include<cstdlib> // 引入的頭文件 int main() { int num = 10; char str[100]; itoa(num, str, 2); //c++中一般用_itoa,用itoa也行, printf("%s\n", str); return 0; }
三)使用字符串流stringstream
引入頭文件#include
1.將八,十六進制轉十進制。 #include<iostream> #include<string> #include<sstream> using namespace std; int main(void) { string s="20"; int a; stringstream ss; ss<<hex<<s; //以16進制讀入流中 ss>>a; //10進制int型輸出 cout<<a<<endl; return 0; } //輸出:32
2.將十進制轉八,十六進制。
#include<cstdio> #include<iostream> #include<string> #include<sstream> using namespace std; int main(void) { string s1,s2; int a=30; stringstream ss; ss<<oct<<a; //10進制轉成八進制讀入流中,再以字符串輸出 ss>>s1; cout<<s1<<endl; //輸出:36 ss.clear(); //不清空可能會出錯。 ss<<hex<<a; //10進制轉成十六進制讀入流中,,再以字符串輸出 ss>>s2; cout<<s2<<endl; //輸出:1e return 0; }