//
Main.c
Hex Conversion
//
Created by Ma C on 15/7/22.
Copyright (c) 2015 BJSXT. All rights reserved.
Requirements: Conversion between decimal to any binary (look up table method).
#include <stdio.h>
hexadecimal conversions
void tohex (int num)
{
int temp;
Char Chs[8]; //define a temporary container with a length of 8,8x4=32 bit bits
int pos=8; //define an index
Char ch[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ',
' 4 ', ' 5 ', ' 6 ', ' 7 ',
' 8 ', ' 9 ', ' A ', ' B ',
' C ', ' d ', ' e ', ' f '};
printf ("Hexadecimal of%d is:", num);
while (num!=0)
{
temp = num & 15;
Chs[--pos]= Ch[temp]; //The data in the table is stored in a temporary container.
num = num >> 4; //Move left four bit lower
};
for (int x=pos;x<8;x++)
{
printf ("%c", chs[x]);
}
printf ("\ n");
}
Octal conversions
void tooct (int num)
{
int temp;
Char chs[11]; //define a temporary container
int pos=11; //define an index
Char ch[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ',
' 4 ', ' 5 ', ' 6 ', ' 7 ',
' 8 ', ' 9 ', ' A ', ' B ',
' C ', ' d ', ' e ', ' f '};
printf ("%d octal is:", num);
while (num!=0)
{
temp = num & 7;
Chs[--pos]= Ch[temp]; //The data in the table is stored in a temporary container.
num = num >> 3; //Move left three bit lower
};
for (int x=pos;x<11;x++)
{
printf ("%c", chs[x]);
}
printf ("\ n");
}
Conversion of binary binary
void tobinary (int num)
{
int temp;
Char chs[32]; //define a temporary container
int pos=32; //define an index
Char ch[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ',
' 4 ', ' 5 ', ' 6 ', ' 7 ',
' 8 ', ' 9 ', ' A ', ' B ',
' C ', ' d ', ' e ', ' f '};
printf ("%d binary is:", num);
while (num!=0)
{
temp = num & 1;
Chs[--pos]= Ch[temp]; //The data in the table is stored in a temporary container.
num = num >> 1; //Move left one bit lower
};
for (int x=pos;x<32;x++)
{
printf ("%c", chs[x]);
}
printf ("\ n");
}
int main (int argc, const char * argv[])
{
Tohex (60);
TOOCT (60);
Tobinary (60);
printf ("\ n");
return 0;
}
C Language: Decimal conversion into other binary (thought: Table method)