C program to convert Hexadecimal to Binary number system

Write a C program to read hexadecimal number from user and convert to binary number system. How to convert hexadecimal number system to binary number system in C.

Example:
Input hexadecimal number: (1A)16
Output decimal number: (26)10

Required knowledge:

Basic C programming, For loop

Hexadecimal number system:

Hexadecimal number system is a base 16 number system. It uses 16 symbols to represent all numbers i.e. 0 1 2 3 4 5 6 7 8 9 A B C D E F

Binary number system:

Binary number system is a base 2 number system. It uses 2 symbols to represent all numbers i.e. 0 and 1.

Algorithm to convert Hexadecimal to Binary

To convert from hexadecimal number to binary number system just group all hexadecimal digits and write corresponding binary value of each hexadecimal digit.

Hexadecimal to Binary conversion

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

Program:

/**
 * C program to convert Hexadecimal to binary number system
 */

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

int main()
{
    char hex[17], bin[65] = "";
    int i = 0;

    /*
     * Reads hexadecimal number from user
     */
    printf("Enter any hexadecimal number: ");
    gets(hex);

    /*
     * Finds binary of each hexadecimal digit
     */
    for(i=0; hex[i]!='\0'; i++)
    {
        switch(hex[i])
        {
            case '0':
                strcat(bin, "0000");
                break;
            case '1':
                strcat(bin, "0001");
                break;
            case '2':
                strcat(bin, "0010");
                break;
            case '3':
                strcat(bin, "0011");
                break;
            case '4':
                strcat(bin, "0100");
                break;
            case '5':
                strcat(bin, "0101");
                break;
            case '6':
                strcat(bin, "0110");
                break;
            case '7':
                strcat(bin, "0111");
                break;
            case '8':
                strcat(bin, "1000");
                break;
            case '9':
                strcat(bin, "1001");
                break;
            case 'a':
            case 'A':
                strcat(bin, "1010");
                break;
            case 'b':
            case 'B':
                strcat(bin, "1011");
                break;
            case 'c':
            case 'C':
                strcat(bin, "1100");
                break;
            case 'd':
            case 'D':
                strcat(bin, "1101");
                break;
            case 'e':
            case 'E':
                strcat(bin, "1110");
                break;
            case 'f':
            case 'F':
                strcat(bin, "1111");
                break;
            default:
                printf("Invalid hexadecimal input.");
        }
    }

    printf("\nHexademial number = %s\n", hex);
    printf("Binary number = %s", bin);

    return 0;
} 
Output
Enter any hexadecimal number: 1a

Hexademial number = 1a
Binary number = 00011010

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

3 comments:

  1. Good site.Except the domain. You may have to go for www.codeforwin.com

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete