C语言程序:
#include
#include
#include
#define MAX 33
char signs[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
/* 十进制数转换为二进制数(字符串表示) */
char *decToBin(long num);
/* 十进制数转换为八进制数(字符串表示) */
char *decToOct(long num);
/* 十进制数转换为十六进制数(字符串表示) */
char *decToHex(long num);
int main()
{
long dec = 123456789;
char *bin = "111010110111100110100010101";
char *oct = "726746425";
char *hex = "75BCD15";
printf("10进制数:\t%ld\n", dec);
printf("2进制数:\t%s\n", decToBin(dec));
printf("8进制数:\t%s\n", decToOct(dec));
printf("16进制数:\t%s\n", decToHex(dec));
return 0;
}
/* 十进制数转换为二进制数(字符串表示) */
char *decToBin(long num)
{
char *arr;
char temp;
int i, n;
arr = (char *)malloc(sizeof(char) * MAX);
n = 0;
while(num > 0)
{
arr[n++] = num % 2 + '0';
num /= 2;
}
for(i=0; i{
temp = arr[i];
arr[i] = arr[n-1-i];
arr[n-1-i] = temp;
}
arr[n] = '\0';
return arr;
}
/* 十进制数转换为八进制数(字符串表示) */
char *decToOct(long num)
{
char *arr;
char temp;
int i, n;
arr = (char *)malloc(sizeof(char) * MAX);
n = 0;
while(num > 0)
{
arr[n++] = num % 8 + '0';
num /= 8;
}
for(i=0; i{
temp = arr[i];
arr[i] = arr[n-1-i];
arr[n-1-i] = temp;
}
arr[n] = '\0';
return arr;
}
/* 十进制数转换为十六进制数(字符串表示) */
char *decToHex(long num)
{
char *arr;
char temp;
int i, n;
arr = (char *)malloc(sizeof(char) * MAX);
n = 0;
while(num > 0)
{
arr[n++] = signs[num % 16];
num /= 16;
}
for(i=0; i{
temp = arr[i];
arr[i] = arr[n-1-i];
arr[n-1-i] = temp;
}
arr[n] = '\0';
return arr;
}
运行测试:
/**
**程序说明:输入一个十进制数,将其转化为二进制数,八进制数,十六进制数
*/
#include
//任意进制转换函数,输入数字n 进制radix
void Hex_to_Others(int n,int radix)
{
int remain; //余数
int i,k; //i,k表示数组下标
int arr[10];
k=-1;
do
{
++k;
remain=n%radix;
arr[k]=remain;
n=n/radix;
}while(n != 0);
for(i=k;i>=0;i--)
{
if(arr[i]>9)
printf("%c",arr[i]+55);
else
printf("%d",arr[i]);
}
printf("\n");
}
int main()
{
int n;
printf("请输入一个十进制数: ");
scanf("%d",&n);
Hex_to_Others(n,2);
Hex_to_Others(n,8);
Hex_to_Others(n,16);
return 0;
}
#include
#include
#define LEN 100
int decimal2Binary (int num) { /*十进制转二进制*/
int rem,temp,i=0;
int s[LEN];
char string[LEN];
temp = num;
while(temp) {
rem=temp%2;
temp=temp/2;
s[i++]=rem;
}
printf("二进制:");
while(i>0)
printf("%d",s[--i]);
printf("\n");
itoa(num,string,2);
printf("使用itoa函数验证:%s\n",string);
printf("\n");
}
int decimal2Octal (int num) { /*十进制转八进制*/
int rem,temp,i=0;
int s[LEN];
char string[LEN];
temp = num;
while(temp) {
rem=temp%8;
temp=temp/8;
s[i++]=rem;
}
printf("八进制:");
while(i>0)
printf("%d",s[--i]);
printf("\n");
itoa(num,string,8);
printf("使用itoa函数验证:%s\n",string);
printf("\n");
}
int decimal2Hexadecimal (int num) { /*十进制转十六进制*/
int temp,j,i=0;
char s[LEN],string[LEN];
temp = num;
while (temp) {
j=temp%16;
if(j>=0&&j<=9)
s[i]= j + '0';
else s[i]=j+87;
temp=temp/16;
i++;
}
s[i]='\0';
printf("十六进制:");
for(i=strlen(s)-1;i>=0;i--)
printf("%c",s[i]);
printf("\n");
itoa(num,string,16);
printf("使用itoa函数验证:%s\n",string);
printf("\n");
}
int main(void) {
int num;
printf("请输入十进制数:\n");
scanf("%d",&num);
printf("\n");
decimal2Binary (num);
decimal2Octal (num);
decimal2Hexadecimal (num);
}
运行结果