十六进制转八进制
作者:互联网
#include<stdio.h> #include<string.h> int two[400005]; int eight[400005]; char six[100005]; int trans(char s)//十六进制转十进制 { if (s >= '0' && s <= '9') return s - '0'; else return s - 'A' + 10; } int main(void) { int n,i; scanf("%d", &n); while (n--) { scanf("%s", six); int num = strlen(six), count,T=0,E=0; for (i = num - 1; i >= 0; i--) { int rec = trans(six[i]); count = 4; while (count--) { two[T++] = rec % 2; // 将十六进制转二进制倒序保存 rec /= 2; } } //int q = 0; //for ( i = 0; i < T; i++) //{ // if (q == 0) {eight[E] += two[i]; q++;} // else if (q == 1) { eight[E] += two[i] * 2; q++; } // else if (q == 2) { eight[E] += two[i] * 2 * 2; q = 0; E++; } //} int cnt = 1; int x = 0; for (i = 0; i < T; i++) { if (cnt == 4 || i == T - 1)//二进制转八进制是三位一个数 { x = cnt * two[i] + x;//逐步求出三位二进制数的十进制数 cnt = 1; eight[E++] = x; x = 0;//当到达第三位时将x清零,继续保存下一个八进制数 } else { x = cnt * two[i] + x; cnt *= 2;//位权 } } while (!eight[E])E--; for (i = E; i >= 0; i--) printf("%d", eight[i]); printf("\n"); } return 0; }
标签:十六进制,cnt,int,two,++,eight,--,八进制 来源: https://www.cnblogs.com/loliconsk/p/14341824.html