21.MFC進制轉換工具
阿新 • • 發佈:2018-01-02
else ima == lib src 技術分享 print gpo void
相關代碼:鏈接: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; 12int 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 //從最高位開始依次計算 40for (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 }
21.MFC進制轉換工具