将七进制数转换为十四进制数

{ int num = 1; for(int i=0; i<exp; i++) { num *= a; } return num; } /* convert a number in seven radix to fourteen radix number */ char * convert7To14(char *src) { int len = strlen(src); int left = src[0] - '0'; int* result = new int[len+1]; for(int i=0; i<len-1; i++) { int exp = pow(2, len-1-i); result[i] = left / exp; left = left % exp; left *= 7; left += (src[i+1] - '0'); } result[len-1] = left; //conver type of int* to type of char* int i = 0; while(result[i] == 0) { i++; } char * res = new char[len-i+1]; int index = 0; for( ; i<len; i++) { if(result[i] > 9) { res[index] = result[i] - 10 + 'A'; } else { res[index] = result[i] + '0'; } index++; } res[index] = '/0'; return res; }

 

优化之前的code:

 

char * convert(char * src) { int len = strlen(src); int num = 0; for(int i=0; i<len; i++) { num *= 7; num += (src[i]-'0'); } printf("convert10 : %d/n", num); int * tmp = new int[10]; int index = 0; int low = 0; while( num != 0) { low = num % 14; num /= 14; tmp[index++] = low; } char * result = new char[index+1]; for(int i=0; i<index; i++) { if(tmp[i] > 9) { result[index-1-i] = tmp[i] - 10 + 'A'; } else { result[index-1-i] = tmp[i] + '0'; } } result[index] = '/0'; return result; }

 

你可能感兴趣的:(优化)