Doing various computer base numbering conversions: decimal, hexadecimal, octal and binary

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

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 various computer base numbering conversions in C programming: decimal, hexadecimal, octal and binary

To show: How to convert different  computer base numbering system such as decimal, binary, octal and hexadecimal in C programming

/* Playing with binary, decimal, hexadecimal and octal conversions */

#include <stdio.h>

/* for exit() */

#include <stdlib.h>

/* for strlen()/wcslen() */

#include <string.h>

/* function prototypes */

/* convert decimal to binary */

void decnumtobin (int *dec);

/* octal conversion function */

void octal(char *octa, int *octares);

/* decimal conversion function */

void decimal(char *deci, int *decires);

/* convert binary to decimal */

void bintodec(void);

int main(void)

{

/* Yes or No value to continue with program */

char go;

/* Yes or No value to proceed to Binary to Decimal function */

char binY;

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 base of ur input(d=dec, h=hex, o=octal): ");

/* scanf("%c", &choice1); */

scanf_s("%c", &choice1, 1);

getchar();

printf("\n");

printf("The entered Number: ");

/* If decimal number */

if ((choice1 == 'd') || (choice1 == 'D'))

{

/* scanf("%d", &numtest); */

scanf_s("%d", &numtest, 1);

getchar();

}

else if ((choice1 == 'h') || (choice1 == 'H'))

{

/* scanf("%x", &numtest); */

scanf_s("%x", &numtest, 1);

getchar();

}

/* If octal number */

else if ((choice1 == 'o') || (choice1 == 'O'))

{

/* scanf("%o", &numtest); */

scanf_s("%o", &numtest, 1);

getchar();

}

/* If no match */

else

{

flag = 1;

printf("Only d, h or o options!\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("Next, enter the base of ur output (d=dec, h=hex, o=octal): ");

/* scanf("%c", &choice2); */

scanf_s("%c", &choice2, 1);

getchar();

/* If decimal number */

if ((choice2 == 'd') || (choice2 == 'D'))

decimal (&choice1, &numtest);

else if ((choice2 == 'h') || (choice2 == 'H'))

/* If octal number */

else if ((choice2 == 'o') || (choice2 == 'O'))

octal (&choice1, &numtest);

/* if nothing matched */

else

{

flag = 1;

printf("Only d, h or o options!");

printf("\nProgram exit...");

exit(0);

}

printf("\n\nAn OPTION\n");

printf("=========\n");

printf("Do you wish to do the binary to decimal conversion?");

printf("\n Y for Yes, and N for no : ");

/* scanf("%c", &binY); */

scanf_s("%c", &binY, 1);

getchar();

/* If Yes... */

if ((binY == 'Y') || (binY == 'y'))

/* Do the binary to decimal conversion */

bintodec();

/* If not, just exit */

else if ((binY != 'y') || (binY != 'Y'))

{

flag = 1;

printf("\nProgram exit...\n");

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 == 'd') || (ch == 'D'))

printf("\nThe number \"%d\" in decimal is equivalent to \"%d\" in decimal.\n", ans, ans);

else if ((ch == 'h') || (ch == 'H'))

printf("\nThe number \"%X\" in hex is equivalent to \"%d\" in decimal.\n", ans, ans);

else if ((ch == 'o') || (ch == 'O'))

printf("\nThe number \"%o\" in octal is equivalent to \"%d\" in decimal.\n", ans, ans);

}

{

int ans = *hexares;

char ch = *hexa;

if ((ch == 'd') || (ch == 'D'))

printf("\nThe number \"%d\" in decimal is equivalent to \"%X\" in hexadecimal.\n", ans, ans);

else if ((ch == 'h') || (ch == 'H'))

printf("\nThe number \"%X\" in hex is equivalent to \"%X\" in hexadecimal.\n", ans, ans);

else if ((ch == 'o') || (ch == 'O'))

printf("\nThe number \"%o\" in octal is equivalent to \"%X\" in hexadecimal.\n", ans, ans);

}

void octal(char *octa, int *octares)

{

int ans = *octares;

char ch = *octa;

if ((ch == 'd') || (ch == 'D'))

printf ("\nThe number \"%d\" in decimal is equivalent to \"%o\" in octal.\n", ans, ans);

else if ((ch == 'h') || (ch == 'H'))

printf("\nThe number \"%X\" in hex is equivalent to \"%o\" in octal. \n", ans, ans);

else if ((ch == 'o') || (ch == 'O'))

printf("\nThe number \"%o\" in octal is equivalent to \"%o\" in octal.\n", ans, ans);

}

void bintodec(void)

{

char buffbin;

char *binary;

int i=0;

int dec = 0;

int z;

printf("Please enter the binary digits, 0 or 1.\n");

/* binary = gets(buffbin); */

binary = gets_s(buffbin, 1024);

i = strlen(binary);

for(z=0; z<i z='1'> 0);

/* Reverse and output binary digits */

printf ("The binary representation is: ");

do

{

printf ("%d", binary[count - 1]);

count--;

} while (count > 0);

printf ("\n");

}

Output example:

Enter the base of ur input(d=dec, h=hex, o=octal): d

The entered Number: 1000

The binary representation is: 1111101000

Next, enter the base of ur output (d=dec, h=hex, o=octal): h

The number "1000" in decimal is equivalent to "3E8" in hexadecimal.

An OPTION

=========

Do you wish to do the binary to decimal conversion?

Y for Yes, and N for no : y

Please enter the binary digits, 0 or 1.

The decimal value of 101010101010111000 is 174776

The program is ready to exit...

Start again? (Y for Yes) : t

-----FINISH-----

Press any key to continue . . .