# Decimal to arbitrary base

Time：2021-9-29

Dear friends

In the afternoon, I recorded the decimal conversion to other arbitrary decimal algorithms. The complete code is implemented as follows. The basic principle uses the rolling division method.

``````1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std;
8
9 string JinZhi10To2Str(int n)
10 {
11     int i;
12     string temp = "";
13     string value = "";
14     i = n;
15     while (i) {
16         //_itoa_s(i % 2,str,10);
17         temp = to_string(i % 2);
18         value = temp + value; // Flashback splicing
19         i /= 2;
20     }
21     return value;
22 }
23
24 string JinZhi10To8Str(int n)
25 {
26     int i;
27     string value = "";
28     string temp = "";
29     i = n;
30     while (i)
31     {
32         temp = to_string(i % 8);
33         value = temp + value;
34         i /= 8;
35     }
36     return value;
37 }
38
39 string JinZhi10To16Str(int n)
40 {
41     int i;
42     string value = "";
43     string temp = "";
44     i = n;
45     while (i) {
46         int m = i % 16;
47         if (m >= 10) {
48             switch (m)
49             {
50             case 10:temp = "A"; break;
51             case 11:temp = "B"; break;
52             case 12:temp = "C"; break;
53             case 13:temp = "D"; break;
54             case 14:temp = "E"; break;
55             case 15:temp = "F"; break;
56             default:
57                 break;
58             }
59             value = temp + value;
60         }
61         else {
62             temp = to_string(m);
63             value = temp + value;
64         }
65         i /= 16;
66     }
67     return value;
68 }
69
70 string JinZhi10To32Str(int n)
71 {
72     string value;
73     string tmp;
74
75     char str[100];
76     for (int i = 3; i >= 0; i--)
77     {
78         int a = 1;
79         switch (i)
80         {
81         case 1: a = 32; break;
82         case 2: a = 32 * 32; break;
83         case 3: a = 32 * 32 * 32; break;
84         }
85
86         tmp = "";
87         int b = n / a;
88         if (b <= 0x0f)
89         {
90
91             _itoa_s(b, str, 16);
92             string s(&str[0], &str[strlen(str)]);
93             transform(s.begin(), s.end(), s.begin(), ::toupper);// Convert to uppercase
94             //transform(str.begin(), str.end(), str.begin(), ::tolower); // Convert to lowercase
95             tmp = s;
96
97             //tmp = to_ string(b);// You can directly convert an integer to a string type and include it in #include
98
99         #if 0
100             tmp.format("%x", b); // Format hexadecimal output in MFC
101         #endif
102
103         }
104         else
105         {
106             // g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v
107             switch (b)
108             {
109             case 16:tmp = "G"; break;
110             case 17:tmp = "H"; break;
111             case 18:tmp = "J"; break;
112             case 19:tmp = "K"; break;
113             case 20:tmp = "L"; break;
114             case 21:tmp = "M"; break;
115             case 22:tmp = "N"; break;
116             case 23:tmp = "P"; break;
117             case 24:tmp = "R"; break;
118             case 25:tmp = "S"; break;
119             case 26:tmp = "T"; break;
120             case 27:tmp = "V"; break;
121             case 28:tmp = "W"; break;
122             case 29:tmp = "X"; break;
123             case 30:tmp = "Y"; break;
124             case 31:tmp = "Z"; break;
125             default:tmp = ""; break;
126             }
127         }
128         value += tmp;
129         n %= a;
130     }
131     return value;
132 }
133
134 string JinZhi10To32Str_1(int n)
135 {
136     int i;
137     string value = "";
138     string temp = "";
139     i = n;
140     while (i) {
141         int m = i % 32;
142         if (m >= 10) {
143             switch (m)
144             {
145             case 10:temp = "A"; break;
146             case 11:temp = "B"; break;
147             case 12:temp = "C"; break;
148             case 13:temp = "D"; break;
149             case 14:temp = "E"; break;
150             case 15:temp = "F"; break;
151             case 16:temp = "G"; break;
152             case 17:temp = "H"; break;
153             case 18:temp = "I"; break;
154             case 19:temp = "J"; break;
155             case 20:temp = "K"; break;
156             case 21:temp = "L"; break;
157             case 22:temp = "M"; break;
158             case 23:temp = "N"; break;
159             case 24:temp = "O"; break;
160             case 25:temp = "P"; break;
161             case 26:temp = "Q"; break;
162             case 27:temp = "R"; break;
163             case 28:temp = "S"; break;
164             case 29:temp = "T"; break;
165             case 30:temp = "U"; break;
166             case 31:temp = "V"; break;
167             default:
168                 break;
169             }
170             value = temp + value;
171         }
172         else {
173             temp = to_string(m);
174             value = temp + value;
175         }
176         i /= 32;
177     }
178     return value;
179 }
180
181 int main()
182 {
183     int m,temp;
184     while (true) {
185 cout < < endl < < "* * * * * * * * * * * *" < < endl;
186         cout << "  *                                      *" << endl;
187 cout < < "* hex 1, hex 10 to hex 2 *" < < endl;
188         cout << "  *                                      *" << endl;
189 cout < < "* 2, 10 to 8 *" < < endl;
190         cout << "  *                                      *" << endl;
191 cout < < "* 3, hexadecimal to hexadecimal *" < < endl;
192         cout << "  *                                      *" << endl;
193 cout < < "* 4, 10 base to 32 base (except I, O, u, V) *" < < endl;
194         cout << "  *                                      *" << endl;
195 cout < < 5, 10 to 32 * "< < endl;
196         cout << "  *                                      *" << endl;
197         cout << "  * * * * * * * * * * * * * * * * * * * **" << endl << endl;
198 cout < < "please select:";
199         cin >> m;
200         switch (m)
201         {
202         case 1:
203 cout < < "decimal number:";
204             cin >> temp;
205 cout < < temp < < convert to binary: < < jinzhi10to2str (Temp) < < endl < < endl;
206             break;
207         case 2:
208 cout < < "decimal number:";
209             cin >> temp;
210 cout < < temp < < convert to octal: < < jinzhi10to8str (Temp) < < endl < < endl;
211             break;
212         case 3:
213 cout < < "decimal number:";
214             cin >> temp;
215 cout < < temp < < convert to hexadecimal: < < jinzhi10to16str (Temp) < < endl < < endl;
216             break;
217         case 4:
218 cout < < "decimal number:";
219             cin >> temp;
220 cout < < temp < < convert to 32 base system: < < jinzhi10to32str (Temp) < < endl < < endl;
221             break;
222         case 5:
223 cout < < "decimal number:";
224             cin >> temp;
225 cout < < temp < < convert to 32 base system: < < jinzhi10to32str_ 1(temp) << endl << endl;
226             break;
227         default:
228 cout < < can only be selected from 1-6 < < endl;
229             break;
230         }
231     }
232 }
233
234
235 #if 0
236
237 #include
238 #include
239 int main()
240 {
241     int num = 32;
242     char str[100];
243     _ itoa_ s(num, str, 32);  // Commonly used in C + +_ Itoa, you can use Itoa,
244     printf("%s\n", str);
245     return 0;
246 }
247
248
249 #include
250 using namespace std;
251 void main()
252 {
253     int n, i, j = 0;
254     int a[1000];
255     cin >> n;
256     i = n;
257     while (i)
258     {
259         a[j] = i % 2;
260         i /= 2;
261         j++;
262
263     }
264     for (i = j - 1; i >= 0; i--)
265         cout << a[i];
266     cout << endl;
267 }
268 #endif``````

Operation screenshot:

## Less sass SCSS stylus of CSS preprocessor

Let’s ask ourselves a question: why do we need a preprocessor?The answer is self-evident, that is, CSS itself has some disadvantages: The syntax is not strong enough to be written nested, resulting in unclear style logic in the project. There is no variable and logical reuse mechanism. When it is necessary to reuse code, you […]