void O_B(int a)
{
int i, j, k, s = 0;
int result = 0;
int p[30];
for(i = 1; a != 0; i *= 8)
{
if (a % 10 > 7)
{
s = 1;
break;
}
else
{
result += (a % 10) * i;
a /= 10;
}
}
for(j = 0; result != 0; j++)
{
p[j] = result % 2;
result /= 2;
}
if(s == 1)
printf("您的输入有误!请重新输入\n");
else
{
printf("\n转换后的数为:");
for(k = j - 1; k >= 0; k--)
printf("%d",p[k]);
printf("\n");
}
}
//8进制转10进制
void O_D(int a)
{
int i, s = 0;
int result = 0;
for(i = 1; a != 0; i *= 8)
{
if(a % 10 > 7)
{
s = 1;
break;
}
else
{
result += (a % 10) * i;
a /= 10;
}
}
if(s == 1)
printf("您的输入有误!请重新输入\n");
else
{
printf("\n转换后的数为:%d\n",result);
}
}
//8进制转16进制
void O_X(int a)
{
int i, j, k, s = 0;
char p[30];
int result = 0;
for (i = 1; a != 0; i *= 8)
{
if(a % 10 > 7)
{
s = 1;
break;
}
else
{
result += (a % 10) * i;
a /= 10;
}
}
for (j = 0; result != 0; j++)
{
p[j] = result % 16;
result = result / 16;
if(p[j] < 10)
p[j] += 48;
else
{
switch(p[j])
{
case 10: p[j]='A';
break;
case 11: p[j]='B';
break;
case 12: p[j]='C';
break;
case 13: p[j]='D';
break;
case 14: p[j]='E';
break;
case 15: p[j]='F';
break;
}
}
}
if(s == 1)
printf("您的输入有误!请重新输入\n");
else
{
printf("\n转换后的数为:");
for (k = j - 1; k >= 0; k--)
printf("%c",p[k]);
printf("\n");
}
}
//十六进制转换为十进制,二进制,八进制.
void X_D(char a[],int k)
{
int i, j, s = 0;
int result = 0;
int b[50];
for (i = 0; i < k; i++)
{
if(a[i] <= '9' && a[i] >= '1')
b[i]=a[i]-48;
else
{
switch(a[i])
{
case 'A': b[i] = 10;
break;
case 'B': b[i] = 11;
break;
case 'C': b[i] = 12;
break;
case 'D': b[i] = 13;
break;
case 'E': b[i] = 14;
break;
case 'F': b[i] = 15;
break;
case 'a': b[i] = 10;
break;
case 'b': b[i] = 11;
break;
case 'c': b[i] = 12;
break;
case 'd': b[i] = 13;
break;
case 'e': b[i] = 14;
break;
case 'f': b[i] = 15;
break;
default:
s = 1;
}
}
}
for (i = 1, j = k - 1; j >= 0; j--, i *=16)
result+=b[j]*i;
if(s == 1)
printf("您的输入有误!请重新输入\n");
else
printf("\n转换后的数为:%d",result);
}
void X_B(char a[],int k)
{
int i,j,s = 0;
int result = 0;
int b[50];
int p[30];
for (i = 0; i < k; i++)
{
if(a[i] <= '9' && a[i] >= '1')
b[i]=a[i]-48;
else
{
switch(a[i])
{
case 'A': b[i] = 10;
break;
case 'B': b[i] = 11;
break;
case 'C': b[i] = 12;
break;
case 'D': b[i] = 13;
break;
case 'E': b[i] = 14;
break;
case 'F': b[i] = 15;
break;
case 'a': b[i] = 10;
break;
case 'b': b[i] = 11;
break;
case 'c': b[i] = 12;
break;
case 'd': b[i] = 13;
break;
case 'e': b[i] = 14;
break;
case 'f': b[i] = 15;
break;
default:
s = 1;
}
}
}
for (i = 1, j = k - 1; j >= 0; j--, i *=16)
result+=b[j]*i;
for (j = 0; result != 0; j++)
{
p[j] = result % 2;
result /= 2;
}
if(s == 1)
printf("您的输入有误!请重新输入\n");
else
{
printf("\n转换后的数为:");
for (k = j - 1; k >= 0; k--)
printf("%d",p[k]);
printf("\n");
}
}
void X_O(char a[],int k)
{
int i,j,s = 0;
int result = 0;
int b[30];
int p[30];
for (i = 0; i < k; i++)
{
if(a[i] <= '9' && a[i] >= '1')
b[i]=a[i]-48;
else
{
switch(a[i])
{
case 'A': b[i] = 10;
break;
case 'B': b[i] = 11;
break;
case 'C': b[i] = 12;
break;
case 'D': b[i] = 13;
break;
case 'E': b[i] = 14;
break;
case 'F': b[i] = 15;
break;
case 'a': b[i] = 10;
break;
case 'b': b[i] = 11;
break;
case 'c': b[i] = 12;
break;
case 'd': b[i] = 13;
break;
case 'e': b[i] = 14;
break;
case 'f': b[i] = 15;
break;
default:
s = 1;
}
}
}
for (i = 1, j = k - 1; j >= 0; j--, i *=16)
result+=b[j]*i;
for (j = 0; result != 0; j++)
{
p[j] = result % 8;
result /= 8;
}
if(s == 1)
printf("您的输入有误!请重新输入\n");
else
{
printf("\n转换后的数为:");
for (k = j - 1; k >= 0; k--)
printf("%d",p[k]);
printf("\n");
}
}
///////以下为: 十进制转换为二进制,八进制,十六进制.
void D_B(int a)
{
int j, k;
int p[30];
for (j = 0; a != 0; j++)
{
p[j] = a % 2;
a /= 2;
}
printf("\n转换后的数为:");
for(k = j - 1; k >= 0; k--)
printf("%d", p[k]);
printf("\n");
}
void D_O(int a)
{
int j, k;
int p[30];
for (j = 0; a != 0; j++)
{
p[j] = a % 8;
a /= 8;
}
printf("\n转换后的数为:");
for(k = j - 1; k >= 0; k--)
printf("%d", p[k]);
printf("\n");
}
void D_X(int a)
{
int j,k;
int p[30];
for (j = 0; a != 0; j++)
{
p[j] = a % 16;
a /= 16;
if(p[j] < 10)
p[j]+=48;
else
{
switch(p[j])
{
case 10: p[j] = 'A';
break;
case 11: p[j] = 'B';
break;
case 12: p[j]='C';
break;
case 13: p[j]='D';
break;
case 14: p[j]='E';
break;
case 15: p[j]='F';
break;
}
}
}
printf("\n转换后的数为:");
for (k = j - 1; k >= 0; k--)
printf("%c",p[k]);
printf("\n");
}