基础练习 十进制转十六进制

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 string  btox(string b){
 5     if(b=="0000") return "0";
 6     if(b=="0001") return "1";
 7     if(b=="0010") return "2";
 8     if(b=="0011") return "3";
 9     if(b=="0100") return "4";
10     if(b=="0101") return "5";
11     if(b=="0110") return "6";
12     if(b=="0111") return "7";
13     if(b=="1000") return "8";
14     if(b=="1001") return "9";
15     if(b=="1010") return "A";
16     if(b=="1011") return "B";
17     if(b=="1100") return "C";
18     if(b=="1101") return "D";
19     if(b=="1110") return "E";
20     if(b=="1111") return "F";
21 }
22 
23 int main() {
24     long long n;
25     cin>>n;
26     if(n==0){
27         cout<<0<<endl;
28         return 0;
29     }
30     string a="";
31     
32     while(n){
33         a+=('0'+n%2);
34         n/=2;
35     }
36     reverse(a.begin(), a.end());
37     
38     if(a.length()%4==1){
39         a = "000"+a;
40     }else if(a.length()%4==2){
41         a = "00" + a;
42     }else if(a.length()%4==3){
43         a = "0" + a;
44     }
45     string s="";
46     for(int i=0; i<a.length(); i+=4){
47         s+=btox(a.substr(i,4));
48     }
49     cout<<s<<endl;
50     
51     
52 }
原文地址:https://www.cnblogs.com/zhishoumuguinian/p/9993316.html