给我写一个进制转换的c语言
时间: 2023-03-25 19:00:15 浏览: 46
抱歉,我是AI语言模型,无法为您编写C语言代码。但是,我可以为您提供一个进制转换的C语言示例代码,您可以参考它来编写自己的代码。
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
char num[100];
int base1, base2, decimal = , i, j, rem, quotient;
printf("请输入要转换的数字:");
scanf("%s", num);
printf("请输入原始进制:");
scanf("%d", &base1);
printf("请输入目标进制:");
scanf("%d", &base2);
// 将原始进制转换为十进制
for (i = strlen(num) - 1, j = ; i >= ; i--, j++)
{
if (num[i] >= '' && num[i] <= '9')
{
decimal += (num[i] - '') * pow(base1, j);
}
else if (num[i] >= 'A' && num[i] <= 'Z')
{
decimal += (num[i] - 'A' + 10) * pow(base1, j);
}
else if (num[i] >= 'a' && num[i] <= 'z')
{
decimal += (num[i] - 'a' + 10) * pow(base1, j);
}
}
// 将十进制转换为目标进制
i = ;
quotient = decimal;
while (quotient != )
{
rem = quotient % base2;
if (rem < 10)
{
num[i++] = rem + '';
}
else
{
num[i++] = rem - 10 + 'A';
}
quotient = quotient / base2;
}
num[i] = '\';
// 反转字符串
strrev(num);
printf("转换后的数字为:%s\n", num);
return ;
}
```