//将一个正整数转换成其他基数的函数
#include <stdio.h>
int convertedNumber [64];
long int numberToConvert;
int base;
int digit = 0;
void getNumberAndBase (void)
{
printf ("请输入你需要转换的数字:\n");
scanf ("%li", &numberToConvert);
printf ("你想转换成几进制? ");
scanf ("%i", &base);
if ( base < 2 || base > 16) {
printf ("!!输入错误,必须是2--16之间的数字!!\n");
base = 10;
}
}
void convertNumber (void)
{
do {
convertedNumber[digit] = numberToConvert % base;
++digit;
numberToConvert /= base;
}
while (numberToConvert != 0);
}
void displayConvertedNumber (void)
{
const char baseDigits [16] =
{'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int nextDigit;
printf ("转换结果是: ");
for (--digit; digit >= 0; --digit){
nextDigit = convertedNumber[digit];
printf ("%c", baseDigits[nextDigit]);
}
printf ("\n");
}
int main (void)
{
void getNumberAndBase (void), displayConvertedNumber (void), convertNumber (void);
getNumberAndBase ();
convertNumber ();
displayConvertedNumber ();
system ("pause");
}
来源:https://www.cnblogs.com/Robin5067/p/12389599.html