Another binary, decimal, hex and octal numbering base conversions C program example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: nil
To do: Doing the conversion of the different computer numbering system binary, decimal, hexadecimal and octal conversion
To show: How to convert different computer numbering base which are binary, decimal, hexadecimal and octal in C programming
/* Doing the binary, decimal, hexadecimal and octal conversion */
#include <stdio.h>
#include <stdlib.h>
/* strlen()/wcslen() */
#include <string.h>
/* function prototypes */
/* decimal conversion function */
void decimal(char *deci, int *decires);
/* convert decimal to binary */
void decnumtobin (int *dec);
int main(void)
{
/* Yes or No value to continue with program */
char go;
char choice1;
char choice2;
/* numtest, value to test with, and pass to functions */
int numtest;
/* value to convert to binary, and call decnumtobin function */
int bintest;
int flag;
flag = 0;
go = 'y';
do
{
printf ("Enter the h for hex input: ");
/* scanf("%c", &choice1); */
scanf_s("%c", &choice1, 1);
getchar();
printf ("\n");
printf ("Enter your hex number lor!: ");
/* If hexadecimal number */
if ((choice1 == 'h') || (choice1 == 'H'))
{
/* scanf ("%x", &numtest); */
scanf_s("%x", &numtest, 1);
getchar();
}
else
{
flag = 1;
printf ("Only h!\n");
printf("Program exit...\n");
exit(0);
}
/* Firstly convert the input 'number' to binary */
bintest = numtest;
decnumtobin(&bintest);
/* output the hex, decimal or octal */
printf ("\n");
printf ("Enter the d for decimal output: ");
/* scanf ("%c", &choice2); */
scanf_s("%c", &choice2, 1);
getchar();
/* If decimal number */
if ((choice2 == 'd') || (choice2 == 'D'))
decimal(&choice1, &numtest);
/* else... */
else
{
flag = 1;
printf("Only d!");
printf("\nProgram exit...");
exit(0);
}
printf ("\n\n");
printf ("The program is ready to exit...\n");
printf ("Start again? (Y for Yes) : ");
/* scanf ("%c", &go); */
scanf_s("%c", &go, 1);
getchar();
/* initialize to NULL */
numtest = '\0';
choice1 = '\0';
choice2 = '\0';
}
while ((go == 'y') || (go == 'Y'));
printf ("-----FINISH-----\n");
return 0;
}
void decimal(char *deci, int *decires)
{
int ans = *decires;
char ch = *deci;
if ((ch == 'h') || (ch == 'H'))
printf ("\nThe number \"%X\" in hex is equivalent to \"%d\" in decimal.\n", ans, ans);
}
void decnumtobin (int *dec)
{
int input = *dec;
int i;
int count = 0;
int binary[128];
do
{
/* Modulus 2 to get 1 or a 0 */
i = input%2;
/* Load Elements into the Binary Array */
binary[count] = i;
/* Divide input by 2 for binary decrement */
input = input/2;
/* Count the binary digits */
count++;
}while (input > 0);
/* Reverse and output binary digits */
printf ("The binary representation is: ");
do
{
printf ("%d", binary[count - 1]);
count--;
if(count == 4)
printf(" ");
} while (count > 0);
printf ("\n");
}
Output example:
Enter the h for hex input: h
Enter your hex number lor!: ABCFE7
The binary representation is: 10101011110011111110 0111
Enter the d for decimal output: d
The number "ABCFE7" in hex is equivalent to "11259879" in decimal.
The program is ready to exit...
Start again? (Y for Yes) : y
Enter the h for hex input: h
Enter your hex number lor!: AB
The binary representation is: 1010 1011
Enter the d for decimal output: d
The number "AB" in hex is equivalent to "171" in decimal.
The program is ready to exit...
Start again? (Y for Yes) : n
-----FINISH-----
Press any key to continue . . .