The Hexadecimal, binary and ASCII equivalent C program example

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:

To do: Doing the hexadecimal, binary and ASCII conversion in C programming

To show: The Hexadecimal, binary and ASCII equivalent C program example

/* Playing with hexadecimal and ascii */

#include <stdio.h>

#include <stdlib.h>

/* strlen()/wcslen() */

#include <string.h>

/* function prototypes */

/* decimal conversion function */

void decimal(int *decires);

/* convert decimal to binary */

void decnumtobin (int *dec);

int main(void)

{

/* Program continuation... */

char go;

/* numtest, value to test with, and pass to functions */

int numtest;

/* value to convert to binary, and call decnumtobin function */

int bintest;

int flag = 0;

go = 'y';

do

{

printf("\nPlaying with hex and ASCII\n");

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

printf("For hex, 0(0) - 1F(32) are non printable/control characters!\n");

printf("For hex > 7F(127) they are extended ASCII characters that are\n");

printf("platform dependent!\n\n");

printf("Enter the hex input: ");

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

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

getchar();

/* Firstly convert the input 'number' to binary */

bintest = numtest;

decnumtobin(&bintest);

decimal (&numtest);

printf("\nStart again? (Y for Yes) : ");

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

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

getchar();

/* initialize to NULL */

numtest = '\0';

}

while ((go == 'y') || (go == 'Y'));

printf("-----FINISH-----\n");

return 0;

}

void decimal(int *decires)

{

int ans = *decires;

/* If < decimal 32... */

if(ans <32> 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:

Playing with hex and ASCII

==========================

For hex, 0(0) - 1F(32) are non printable/control characters!

For hex > 7F(127) they are extended ASCII characters that are

platform dependent!

Enter the hex input: ABDFE

The binary representation is: 1010101111011111 1110

The number "ABDFE" in hex is equivalent to "?" ascii character.

Start again? (Y for Yes) : y

Playing with hex and ASCII

==========================

For hex, 0(0) - 1F(32) are non printable/control characters!

For hex > 7F(127) they are extended ASCII characters that are

platform dependent!

Enter the hex input: 56DFAC

The binary representation is: 1010110110111111010 1100

The number "56DFAC" in hex is equivalent to "1/4" ascii character.

Start again? (Y for Yes) : n

-----FINISH-----

Press any key to continue . . .