相关代码:链接:https://pan.baidu.com/s/1pKVVUZL 密码:e3vf
1 #include <stdlib.h> 2 #include <stdio.h> 3 #include <string.h> 4 #include <string.h> 5 #include <math.h> 6 7 //从2进制转到10进制 8 int from_2to10(char res[30]) 9 { 10 printf("%s", res); 11 int result = 0; 12 int len = strlen(res); 13 //从最高位开始依次计算 14 for (int i = 0; i < len; i++) 15 { 16 result += (res[i] - ‘0‘) * pow(2.0, len - i - 1); 17 } 18 19 return result; 20 } 21 //从8进制转到10进制 22 int from_8to10(char res[30]) 23 { 24 int result = 0; 25 int len = strlen(res); 26 //从最高位开始依次计算 27 for (int i = 0; i < len; i++) 28 { 29 result += (res[i] - ‘0‘) * pow(8.0, len - i - 1); 30 } 31 32 return result; 33 } 34 //从16进制转到10进制 35 int from_16to10(char res[30]) 36 { 37 int result = 0; 38 int len = strlen(res); 39 //从最高位开始依次计算 40 for (int i = 0; i < len; i++) 41 { 42 if (res[i] - ‘0‘ < 10) 43 { 44 result += (res[i] - ‘0‘) * pow(16.0, len - i - 1); 45 } 46 else 47 { 48 result += (10 + res[i] - ‘A‘) * pow(16.0, len - i - 1); 49 } 50 } 51 52 return result; 53 } 54 //10进制转到2进制 55 void to2(int num, char res[30], int i) 56 { 57 if (num == 0) 58 { 59 return; 60 } 61 else 62 { 63 res[i] = ‘0‘ + num % 2; 64 to2(num / 2, res, i + 1); 65 } 66 } 67 //10进制转到8进制 68 void to8(int num, char res[30], int i) 69 { 70 if (num == 0) 71 { 72 return; 73 } 74 else 75 { 76 res[i] = ‘0‘ + num % 8; 77 to8(num / 8, res, i + 1); 78 } 79 } 80 //10进制转到16进制 81 void to16(int num, char res[30], int i) 82 { 83 if (num == 0) 84 { 85 return; 86 } 87 else 88 { 89 if (num % 16 >= 0 && num % 16 <= 9) 90 { 91 res[i] = ‘0‘ + num % 16; 92 } 93 else 94 { 95 res[i] = ‘A‘ + num % 16 - 10; 96 } 97 98 to16(num / 16, res, i + 1); 99 } 100 }