HexaDecimal to Binary C Program

Numbers with base 16 are called Hexa Decimal number (i.e. 0,1....,9,A,B,C,D,E,F) and numbers with base 2 are called Binary Numbers (i.e. 0, 1).
HexaDecimal to Binary Conversation is very Easy ! All you need is to keep in mind the following table:

Decimal
HexaDecimal
Binary
0
0
0000
1
1
0001
2
2
0010
3
3
0011
4
4
0100
5
5
0101
6
6
0110
7
7
0111
8
8
1000
9
9
1001
10
A
1010
11
B
1011
12
C
1100
13
D
1101
14
E
1110
15
F
1111

Now replace HexaDecimal digits with equivalent binary value. Your HexaDecimal number will now be converted into Binary Number. For example:
(A62)16 = (101001100010)2 Here is a C Program on how to convert HexaDecimal Numbers into Binary Number:

#include <stdio.h>
#include <string.h>

int main()
{
    char hex[1000];
    int i, j;
    printf("Enter a HexaDecimal Number: ");
    scanf("%s",hex);
    j = strlen(hex);
    printf("Equivalent Binary Number is: ");
    for(i=0;i<j;i++){
        switch(hex[i]){
        case '0':
            printf("0000");
            break;
        case '1':
            printf("0001");
            break;
        case '2':
            printf("0010");
            break;
        case '3':
            printf("0011");
            break;
        case '4':
            printf("0100");
            break;
        case '5':
            printf("0101");
            break;
        case '6':
            printf("0110");
            break;
        case '7':
            printf("0111");
            break;
        case '8':
            printf("1000");
            break;
        case '9':
            printf("1001");
            break;
        case 'A':
            printf("1010");
            break;
        case 'B':
            printf("1011");
            break;
        case 'C':
            printf("1100");
            break;
        case 'D':
            printf("1101");
            break;
        case 'E':
            printf("1110");
            break;
        case 'F':
            printf("1111");
            break;
        case 'a':
            printf("1010");
            break;
        case 'b':
            printf("1011");
            break;
        case 'c':
            printf("1100");
            break;
        case 'd':
            printf("1101");
            break;
        case 'e':
            printf("1110");
            break;
        case 'f':
            printf("1111");
            break;
        }
    }
}

0 comments :

Post a Comment

Spam comments will be deleted. :)

 
Loading...
TOP