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

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 string xtob(char x) {
 5     switch(x) {
 6         case '0':
 7             return "0000";
 8         case '1':
 9             return "0001";
10         case '2':
11             return "0010";
12         case '3':
13             return "0011";
14         case '4':
15             return "0100";
16         case '5':
17             return "0101";
18         case '6':
19             return "0110";
20         case '7':
21             return "0111";
22         case '8':
23             return "1000";
24         case '9':
25             return "1001";
26         case 'A':
27             return "1010";
28         case 'B':
29             return "1011";
30         case 'C':
31             return "1100";
32         case 'D':
33             return "1101";
34         case 'E':
35             return "1110";
36         case 'F':
37             return "1111";
38 
39     }
40 }
41 string btoo(string b){
42     if(b=="000") return "0";
43     if(b=="001") return "1";
44     if(b=="010") return "2";
45     if(b=="011") return "3";
46     if(b=="100") return "4";
47     if(b=="101") return "5";
48     if(b=="110") return "6";
49     if(b=="111") return "7";
50 }
51 
52 int main() {
53     int n;
54     cin>>n;
55     while(n--){
56     
57     string x, b="", o="";
58     cin>>x;
59     for(int i=0; i<x.length(); i++){
60         b+=xtob(x[i]);
61     }
62     if(b.length()%3==1){
63         b = "00"+b;
64     }else if(b.length()%3==2){
65         b = "0" + b;
66     }
67     
68     for(int i=0; i<b.length(); i+=3){
69         string s=b.substr(i, 3);
70         o+=btoo(s);
71     }
72     int i=0; 
73     while(o[i]=='0'){
74         i++;
75     }
76     o=o.substr(i);
77     cout<<o<<endl;
78     
79 }
80 
81 
82 }
原文地址:https://www.cnblogs.com/zhishoumuguinian/p/9993327.html