#include <stdio.h>
#define M sizeof(unsigned int)*9
/*将无符号整数 n 转换成 d (2<= d <=16)进制所表示的字符串 s */
int translation(unsigned n, int d, char s[])
{
static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; /* 总共36进制 */
char buf[M+1];
int j, i = M;
if( d < 2 || d > 36) {
s[0] = '\0'; /* 不合理进制数, 使 s 为空字符串*/
return 0; /* 不合理进制数, 函数返回 0 */
}
buf = '\0';
do {
buf[--i] = digits[n%d]; /*得出最低位,将对应字符填入对应数组中*/
n /= d;
}while(n);
for (j = 0; (s[j] = buf) != 0; j++, i++); /*拷贝结果*/
return j;
}
int main()
{
unsigned int num = 0;
int scale[] = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,1};
char str[64];
int i;
printf ("Input a number to ranslate:");
scanf("%d", &num);
printf ("You input is %d\n", num);
printf ("The translation result are:\n");
for (i = 0; i < sizeof(scale)/sizeof(scale[0]); i++) {
if(translation(num, scale, str))
printf("%5d = %s(%d)\n", num, str, scale);
else
printf("%5d => (%d) Error! \n", num, scale);
}
return 0;
}
beyes@linux-xh53:~/c/math> ./change
Input a number to ranslate:385065335
You input is 385065335
The translation result are:
385065335 = 10110111100111010000101110111(2)
385065335 = 222211120100100002(3)
385065335 = 112330322011313(4)
385065335 = 1242034042320(5)
385065335 = 102113141515(6)
385065335 = 12354000314(7)
385065335 = 2674720567(8)
385065335 = 884510302(9)
385065335 = 385065335(10)
385065335 = 1883a5316(11)
385065335 = a8b5a89b(12)
385065335 = 61a12915(13)
385065335 = 391d7d0b(14)
385065335 = 23c13675(15)
385065335 = 16f3a177(16)
385065335 = fg36e01(17)
385065335 = b5e254b(18)
385065335 = 839e2ib(19)
385065335 = 606d36f(20)
385065335 = 4a5k50b(21)
385065335 = 38fh3bh(22)
385065335 = 2dj07i2(23)
385065335 = 208ek4n(24)
385065335 = 1eaj4da(25)
385065335 = 16agf75(26)
385065335 = qmf992(27)
385065335 = mad6lb(28)
385065335 = imce10(29)
385065335 = fpbkb5(30)
385065335 = ddthad(31)
385065335 = bf78bn(32)
385065335 = 9rn0bh(33)
385065335 = 8g53h1(34)
385065335 = 7bl41p(35)
385065335 = 6d9abb(36)
385065335 => (1) Error!
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |