利用位操作以及掩码进行进制间的相互转换。
例一:将输入的一个整型数,转换成相应的二进制数
#include <stdio.h>
void int2bin(int num)
{
int i;
int temp;
int mask = 1;
for(i = 31;i >= 0;i --)
{
temp = ((num >> i) & mask);
printf("%d",temp);
if(i % 4 == 0)
{
printf(" ");
}
}
printf("\n");
}
int main()
{
int num;
printf("Please input an integer:\n");
scanf("%d",&num);
int2bin(num);
return 0;
}
调试结果如下:
Please input an integer:
2345
0000 0000 0000 0000 0000 1001 0010 1001
上面的程序是将输入的整型数进行左移,我在下面试一下用右移的方法实现该功能
#include <stdio.h>
void int2bin(int num)
{
int i;
unsigned int temp;
unsigned int mask = 0x80000000;
for(i = 1;i <= 32;i ++)
{
temp = (unsigned int)((unsigned int)(num << i) & mask);
printf("%d",temp);
if(i % 4 == 0)
{
printf(" ");
}
}
printf("\n");
}
int main()
{
int num;
printf("Please input an interger:\n");
scanf("%d",&num);
int2bin(num);
return 0;
}
调试结果为:
Please input an integer:
2345
0000 0000 0000 0000 0000 1001 0010 1001
例二:将整型数转换成相应的八进制数
#include <stdio.h>
#define MAX_SIZE 1024
void int2oct(int num)
{
int i;
int temp1;
int temp = 1;
int step = -1;
char oct[MAX_SIZE];
oct[0] = '0';
while(num - temp >= 0)
{
temp = temp * 8;
step ++;
}
temp = temp / 8;
for(i = 1;i <= step + 1;i ++,temp = temp / 8)
{
temp1 = num / temp;
num = num - temp * temp1;
oct[i] = temp1 + '0';
}
oct[i] = '\0';
printf("The oct is:%s.\n",oct);
}
int main()
{
int num;
printf("Please input an integer:\n");
scanf("%d",&num);
int2oct(num);
return 0;
}
调试结果为:
Please input an integer:
36475
The oct is:0107173.
例三:将整型数转换成相应的十六进制数
#include <stdio.h>
void int2hex(int num)
{
int i;
int temp;
int mask = 0xf;
char hex[11] = {0};
hex[0] = '0';
hex[1] = 'x';
for(i = 0;i < 8;i ++)
{
temp = num & mask;
num = num >> 4;
if(temp >= 10)
{
hex[9 - i] = temp - 10 + 'a';
}
else
{
hex[9 - i] = temp + '0';
}
}
hex[10] = '\0';
printf("The hex is:%s.\n",hex);
}
int main()
{
int num;
printf("Please input an integer:\n");
scanf("%d",&num);
int2hex(num);
return 0;
}
调试结果为:
Please input an integer:
2351
The hex is:0x0000092f.
例四:将二进制数转换成相应的八进制数
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 1024
char * bin2oct(char * bin,int len)
{
int i;
int j;
int oct_len;
int num = 0;
int count = 0;
int temp = 1;
static char oct[MAX_SIZE];
oct[0] = '0';
if(len % 3 == 0) //判断八进制数是几位
{
oct_len = len / 3;
}
else
{
oct_len = (len / 3) + 1;
}
j = oct_len;
for(i = len - 1;i >= 0;i --)
{
if(count < 3)
{
num = (bin[i] - '0') * temp + num;
temp = temp * 2;
count ++;
if(i == 0)
{
oct[1] = num + '0';
}
}
else
{
oct[j] = num + '0';
num = 0;
temp = 1;
count = 0;
i ++; //再返回一步
j --;
}
}
oct[oct_len + 1] = '\0';
return oct;
}
int main()
{
int len;
char bin[MAX_SIZE];
printf("Please input the bindary:\n");
scanf("%s",bin);
len = strlen(bin);
char * oct = bin2oct(bin,len);
printf("The oct of the binary is:%s.\n",oct);
return 0;
}
调试结果为:
Please input the bindary:
1101011100
The oct of the binary is:01534.
其实二进制转换成八进制有两种方法,一种是将二进制先转换成十进制数再转换成八进制数,我在下面将给大家列出;另一种就是我上面所给出的程序,直接从二进制变换到八进制,即从最低位开始,每三个二进制数为一组,转换成相应的八进制数,如果二进制的个数不是三的倍数,则直接前若干个数值输出位相应的八进制数,相当于最高位补0,保证二进制的个数是三的倍数。
another:
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 1024
char * bin2oct(char * bin)
{
int i;
int len;
int num;
int ten = 0;
int temp = 1;
int temp1 = 1;
int step = -1;
static char oct[MAX_SIZE];
len = strlen(bin);
while(*bin != '\0')
{
ten = ten + (* (bin + len - 1) - '0') * temp;
temp = temp * 2;
bin ++;
len = len - 2;
}
while(ten - temp1 >= 0)
{
temp1 = temp1 * 8;
step ++;
}
temp1 = temp1 / 8;
oct[0] = '0';
for(i = 1;i <= step + 1;i ++,temp1 = temp1 / 8)
{
num = ten / temp1;
ten = ten - num * temp1;
oct[i] = num + '0';
}
oct[i] = '\0';
return oct;
}
int main()
{
char bin[MAX_SIZE];
printf("Please input the binary:\n");
scanf("%s",bin);
char * oct = bin2oct(bin);
printf("The oct of the binary is:%s.\n",oct);
return 0;
}
调试结果为:
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 1024
char * bin2oct(char * bin)
{
int i;
int len;
int num;
int ten = 0;
int temp = 1;
int temp1 = 1;
int step = -1;
static char oct[MAX_SIZE];
len = strlen(bin);
while(*bin != '\0')
{
ten = ten + (* (bin + len - 1) - '0') * temp;
temp = temp * 2;
bin ++;
len = len - 2;
}
while(ten - temp1 >= 0)
{
temp1 = temp1 * 8;
step ++;
}
temp1 = temp1 / 8;
oct[0] = '0';
for(i = 1;i <= step + 1;i ++,temp1 = temp1 / 8)
{
num = ten / temp1;
ten = ten - num * temp1;
oct[i] = num + '0';
}
oct[i] = '\0';
return oct;
}
int main()
{
char bin[MAX_SIZE];
printf("Please input the binary:\n");
scanf("%s",bin);
char * oct = bin2oct(bin);
printf("The oct of the binary is:%s.\n",oct);
return 0;
}
Please input the bindary:
101011101011
The oct of the binary is:05353.
Please input the bindary:
101011101011
The oct of the binary is:05353.
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 1024
int bin2int(char * bin,int len)
{
int i;
int temp1;
int num = 0;
int temp = 1;
for(i = len - 1;i >= 0;i --)
{
temp1 = *(bin + i) - '0';
num = num + temp1 * temp;
temp = temp * 2;
}
return num;
}
int main()
{
int num;
int len;
char bin[MAX_SIZE];
printf("Please input the binary:\n");
scanf("%s",bin);
len = strlen(bin);
num = bin2int(bin,len);
printf("The integer of the binary is:%d.\n",num);
return 0;
}
调试结果为:
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 1024
int bin2int(char * bin,int len)
{
int i;
int temp1;
int num = 0;
int temp = 1;
for(i = len - 1;i >= 0;i --)
{
temp1 = *(bin + i) - '0';
num = num + temp1 * temp;
temp = temp * 2;
}
return num;
}
int main()
{
int num;
int len;
char bin[MAX_SIZE];
printf("Please input the binary:\n");
scanf("%s",bin);
len = strlen(bin);
num = bin2int(bin,len);
printf("The integer of the binary is:%d.\n",num);
return 0;
}
Please input the binary:
1101110101011
The integer of the binary is:7083.
例六:将二进制数转换成相应的十六进制数
Please input the binary:
1101110101011
The integer of the binary is:7083.
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 1024
char * bin2hex(char * bin,int len_bin)
{
int i;
int j;
int len_hex;
int temp = 1;
int temp1 = 0;
int count = 0;
static char hex[MAX_SIZE];
hex[0] = '0';
hex[1] = 'x';
if(len_bin % 4 == 0)
{
len_hex = len_bin / 4;
}
else
{
len_hex = len_bin / 4 + 1;
}
j = len_hex + 1;
for(i = len_bin - 1;i >= 0;i --)
{
if(count < 4)
{
temp1 = (bin[i] - '0') * temp + temp1;
temp = temp * 2;
count ++;
if(i == 0)
{
hex[2] = temp1 + '0';
}
}
else
{
if(temp1 >= 10)
{
hex[j] = temp1 - 10 + 'a';
}
else
{
hex[j] = temp1 + '0';
}
count = 0;
temp = 1;
temp1 = 0;
i ++;
j --;
}
}
hex[len_hex + 2] = '\0';
return hex;
}
int main()
{
int len_bin;
char bin[MAX_SIZE];
printf("Please input the binary:\n");
scanf("%s",bin);
len_bin = strlen(bin);
char * hex = bin2hex(bin,len_bin);
printf("The hex of bin is:%s.\n",hex);
return 0;
}
调试结果为:
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 1024
char * bin2hex(char * bin,int len_bin)
{
int i;
int j;
int len_hex;
int temp = 1;
int temp1 = 0;
int count = 0;
static char hex[MAX_SIZE];
hex[0] = '0';
hex[1] = 'x';
if(len_bin % 4 == 0)
{
len_hex = len_bin / 4;
}
else
{
len_hex = len_bin / 4 + 1;
}
j = len_hex + 1;
for(i = len_bin - 1;i >= 0;i --)
{
if(count < 4)
{
temp1 = (bin[i] - '0') * temp + temp1;
temp = temp * 2;
count ++;
if(i == 0)
{
hex[2] = temp1 + '0';
}
}
else
{
if(temp1 >= 10)
{
hex[j] = temp1 - 10 + 'a';
}
else
{
hex[j] = temp1 + '0';
}
count = 0;
temp = 1;
temp1 = 0;
i ++;
j --;
}
}
hex[len_hex + 2] = '\0';
return hex;
}
int main()
{
int len_bin;
char bin[MAX_SIZE];
printf("Please input the binary:\n");
scanf("%s",bin);
len_bin = strlen(bin);
char * hex = bin2hex(bin,len_bin);
printf("The hex of bin is:%s.\n",hex);
return 0;
}
Please input the binary:
1110100101011
The hex of bin is:0x1d2b.
Please input the binary:
1110100101011
The hex of bin is:0x1d2b.
二进制转换成十六进制数跟二进制数转换成八进制数相同,也是有两种方法:一种是上面程序的方法,即将二进制数直接转换成十六进制数,另一个就是先将二进制数转换成十进制数,再将十进制数转换成十六进制数。
another:
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 1024
char * bin2hex(char * bin,int len_bin)
{
int i;
int len_hex;
int ten = 0;
int temp = 1;
int temp1 = 1;
int step = -1;
static char hex[MAX_SIZE];
while(*bin != '\0')
{
ten = (*(bin + len_bin - 1) - '0') * temp + ten;
temp = temp * 2;
bin ++;
len_bin = len_bin - 2;
}
while(ten - temp1 >= 0)
{
temp1 = temp1 * 16;
step ++;
}
temp1 = temp1 / 16;
len_hex = step + 1;
hex[0] = '0';
hex[1] = 'x';
for(i = 2;i <= len_hex + 1;i ++)
{
if(ten / temp1 >= 10)
{
hex[i] = (ten / temp1) - 10 + 'a';
}
else
{
hex[i] = (ten /temp1) + '0';
}
ten = ten - (ten / temp1) * temp1;
temp1 = temp1 / 16;
}
hex[i] = '\0';
return hex;
}
int main()
{
int len_bin;
char bin[MAX_SIZE];
printf("Please input the binary:\n");
scanf("%s",bin);
len_bin = strlen(bin);
char * hex = bin2hex(bin,len_bin);
printf("The hex of the binary is:%s.\n",hex);
return 0;
}
调试结果为:
Please input the binary:
11110101010101011101101
The hex of the binary is:0x7aaaed.
例七:将八进制数转换成相应的二进制数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SIZE 1024
char * oct2bin(char * oct,int len_oct)
{
if(oct == NULL || &len_oct == NULL)
{
printf("Error!\n");
exit(1);
}
int i;
int j;
int temp;
int num;
static char bin[MAX_SIZE];
for(i = len_oct;i >= 1;i--)
{
temp = 4;
num = oct[i] - '0';
for(j = 0;j < 3;j ++)
{
bin[i * 3 - (3 - j)] = (num / temp) + '0';
num = num - (num / temp) * temp;
temp = temp / 2;
}
}
return bin;
}
int main()
{
int len_oct;
char oct[MAX_SIZE];
printf("Please input the oct:\n");
scanf("%s",oct);
len_oct = strlen(oct) - 1;
char * res = oct2bin(oct,len_oct);
printf("The binary of oct is:%s.\n",res);
return 0;
}
调试结果为:
Please input the oct:
067236354
The binary of oct is:110111010011110011101100.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SIZE 1024
int oct2int(char * oct,int len_oct)
{
if(oct == NULL || &len_oct == NULL)
{
printf("Error!\n");
exit(1);
}
int i;
int ten = 0;
int temp = 1;
for(i = len_oct;i >= 1;i--)
{
ten = ten + (oct[i] - '0') * temp;
temp = temp * 8;
}
return ten;
}
int main()
{
int ten;
int len_oct;
char oct[MAX_SIZE];
printf("Please input the oct:\n");
scanf("%s",oct);
len_oct = strlen(oct) - 1;
ten = oct2int(oct,len_oct);
printf("The ten of oct is:%d.\n",ten);
return 0;
}
Please input the oct:
012345
The ten of oct is:5349.
例九:将八进制数转换成相应的十六进制数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SIZE 1024
char * oct2hex(char * oct,int len_oct)
{
if(oct == NULL || &len_oct == NULL)
{
printf("Error!\n");
exit(1);
}
int i;
int j;
int num;
int ten = 0;
int temp = 1;
int tmp1 = 1;
int step = -1;
static char hex[MAX_SIZE];
hex[0] = '0';
hex[1] = 'x';
for(i = len_oct;i >= 1;i--)
{
num = oct[i] - '0';
ten = ten + num * temp;
temp = temp * 8;
}
while(ten - tmp1 >= 0)
{
step++;
tmp1 = tmp1 * 16;
}
tmp1 = tmp1 / 16;
for(j = step + 2;j >= 2;j--)
{
if(ten / tmp1 >= 1 && ten / tmp1 <= 9)
{
hex[j] = ten / tmp1 + '0';
}
else
{
hex[j] = ten / tmp1 - 10 + '0';
}
ten = ten - (ten / tmp1) * tmp1;
tmp1 = tmp1 / 16;
}
hex[step + 3] = '\0';
return hex;
}
int main()
{
int len_oct;
char oct[MAX_SIZE];
printf("Please input the oct:\n");
scanf("%s",oct);
len_oct = strlen(oct) - 1;
char * hex = oct2hex(oct,len_oct);
printf("The hex of oct is:%s.\n",hex);
return 0;
}
调试结果为:
Please input the oct:
0234567
The hex of oct is:0x77931.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SIZE 1024
char * hex2bin(char * hex,int len_hex)
{
int i;
int j;
int num;
int temp;
static char bin[MAX_SIZE];
for(i = len_hex + 1;i >= 2;i--)
{
if(hex[i] >= '0' && hex[i] <= '9')
{
num = hex[i] - '0';
}
else
{
num = hex[i] - 'a' + 10;
}
temp = 8;
for(j = 0;j < 4;j ++)
{
bin[4 * (i - 1) - (4 - j)] = num / temp + '0';
num = num - (num / temp) * temp;
temp = temp / 2;
}
}
return bin;
}
int main()
{
int len_hex;
char hex[MAX_SIZE];
printf("Please input the hex:\n");
scanf("%s",hex);
len_hex = strlen(hex) - 2;
char * bin = hex2bin(hex,len_hex);
printf("The bin of hex is:%s.\n",bin);
return 0;
}
调试结果为:
Please input the hex:
0xad12df7
The bin of hex is:1010110100010010110111110111.
例十一:将十六进制转换成相应的十进制数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SIZE 1024
int hex2ten(char * hex,int len_hex)
{
int i;
int num;
int temp = 1;
int ten = 0;
for(i = len_hex + 1;i >= 2;i--)
{
if(hex[i] >= '0' && hex[i] <= '9')
{
num = hex[i] - '0';
}
else
{
num = hex[i] - 'a' + 10;
}
ten = ten + num * temp;
temp = temp * 16;
}
return ten;
}
int main()
{
int ten;
int len_hex;
char hex[MAX_SIZE];
printf("Please input the hex:\n");
scanf("%s",hex);
len_hex = strlen(hex) - 2;
ten = hex2ten(hex,len_hex);
printf("Ten of hex is:%d.\n",ten);
return 0;
}
调试结果为:
Please input the hex:
0x77
Ten of hex is:119.
例十二:将十六进制数转换成相应的八进制数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SIZE 1024
char * hex2oct(char * hex,int len_hex)
{
if(hex == NULL || &len_hex == NULL)
{
printf("Error!\n");
exit(1);
}
int i;
int j;
int num;
int ten = 0;
int temp = 1;
int tmp = 1;
int step = -1;
static char oct[MAX_SIZE];
oct[0] = '0';
for(i = len_hex + 1;i >= 2;i--)
{
if(hex[i] >= '0' && hex[i] <= '9')
{
num = hex[i] - '0';
}
else
{
num = hex[i] - 'a' + 10;
}
ten = ten + num * temp;
temp = temp * 16;
}
while(ten - tmp >= 0)
{
step++;
tmp = tmp * 8;
}
tmp = tmp / 8;
for(j = 1;j <= step + 1;j++)
{
oct[j] = (ten / tmp) + '0';
ten = ten - tmp * (ten / tmp);
tmp = tmp / 8;
}
oct[step + 2] = '\0';
return oct;
}
int main()
{
int len_hex;
char hex[MAX_SIZE];
printf("Please input the hex:\n");
scanf("%s",hex);
len_hex = strlen(hex) - 2;
char *oct = hex2oct(hex,len_hex);
printf("The oct of the hex is:%s.\n",oct);
return 0;
}
调试结果为:
Please input the hex:
0xfadff2f
The oct of the hex is:01753377457.