十进制转任意进制
各位亲:
下午没事就记录了十进制转换成其他任意进制算法完整代码实现如下,基本原理运用辗转相除法。
1 #include<iostream> 2 #include<cstring> 3 #include<string> 4 #include<string.h> 5 #include<stdio.h> 6 #include <algorithm> 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; //倒叙拼接 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);//转换成大写字母 94 //transform(str.begin(), str.end(), str.begin(), ::tolower); //转换成小写字母 95 tmp = s; 96 97 //tmp = to_string(b);//可以直接将整型转换成字符串类型包含于#include<string> 98 99 #if 0 100 tmp.format("%x", b); //mfc中格式化16进制输出 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 << " * 1、10进制转2进制 *" << endl; 188 cout << " * *" << endl; 189 cout << " * 2、10进制转8进制 *" << endl; 190 cout << " * *" << endl; 191 cout << " * 3、10进制转16进制 *" << endl; 192 cout << " * *" << endl; 193 cout << " * 4、10进制转32进制(i,o,u,v除外) *" << endl; 194 cout << " * *" << endl; 195 cout << " * 5、10进制转32进制 *" << endl; 196 cout << " * *" << endl; 197 cout << " * * * * * * * * * * * * * * * * * * * **" << endl << endl; 198 cout << " 请选择:"; 199 cin >> m; 200 switch (m) 201 { 202 case 1: 203 cout << "十进制数:"; 204 cin >> temp; 205 cout << temp << "转换成2进制:" << JinZhi10To2Str(temp) << endl << endl; 206 break; 207 case 2: 208 cout << "十进制数:"; 209 cin >> temp; 210 cout << temp << "转换成8进制:" << JinZhi10To8Str(temp) << endl << endl; 211 break; 212 case 3: 213 cout << "十进制数:"; 214 cin >> temp; 215 cout << temp << "转成16进制:" << JinZhi10To16Str(temp) << endl << endl; 216 break; 217 case 4: 218 cout << "十进制数:"; 219 cin >> temp; 220 cout << temp << "转换成32进制:" << JinZhi10To32Str(temp) << endl << endl; 221 break; 222 case 5: 223 cout << "十进制数:"; 224 cin >> temp; 225 cout << temp << "转换成32进制:" << JinZhi10To32Str_1(temp) << endl << endl; 226 break; 227 default: 228 cout << "只能在1-6中选择" << endl; 229 break; 230 } 231 } 232 } 233 234 235 #if 0 236 237 #include<cstdio> 238 #include<cstdlib> 239 int main() 240 { 241 int num = 32; 242 char str[100]; 243 _itoa_s(num, str, 32); //c++中一般用_itoa,用itoa也行, 244 printf("%s\n", str); 245 return 0; 246 } 247 248 249 #include<iostream> 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
运行截图: