This blog is under construction

Thursday 4 July 2013

C Program to convert hexadecimal to binary

How to convert hexadecimal value to binary equivalent?
Hexadecimal value 0x5f5
0x5f5 => (5)(15)(5) => (0101) (1111) (0101) => 010111110101
  • Find decimal value for each digit.
  • Convert decimal value to binary
  • Append the binary outputs.

Write a C program to convert Hexadecimal Value to Binary.


  #include <stdio.h>
  #include <string.h>
  #define MAXBINARYDIG 4

  char output[256];

  /* finds binary equivalent for given decimal value and update o/p */
  void decimalToBinary(int data) {
        int res = 0, i = 0, j = 0, mod, count = MAXBINARYDIG - 1;
        char val[MAXBINARYDIG + 1];

        /*
         * In our case, max length of binary equivalent of
         * given decimal value is 4.  So, we are storing '0'
         * at each index of val. val[] = {'0', '0', '0', '0'}
         */
        memset(val, '0', MAXBINARYDIG);
        /* convert decimal to binary format */
        while (data > 0) {
                mod = data % 2;
                val[count--] = mod + '0';
                data = data / 2;
        }
        /* terminate the string with null character */
        val[MAXBINARYDIG] = '\0';

        /* update the output */
        strcat(output, val);
  }

  int main() {
        char input[10], ch;
        int value = 0, power = 0, i = 0, j = 0, res;

        /* get the hexadecimal value from the user */
        printf("Enter your hexadecimal value:");
        fgets(input, 10, stdin);
        input[strlen(input) - 1] = '\0';

        memset(output, 0, 256);
        /* convert hexadecimal value to decimal */
        for (i = 0; i < strlen(input); i++) {
                ch = input[i];
                if (ch >='0'&& ch <= '9') {
                        /*
                         * input character is any value from 0 to 9
                         */
                        decimalToBinary(ch-'0');
                } else if (ch >= 'a' && ch <= 'f') {
                        /*
                         * input character is any value from a to f
                         * a is 10, b is 11... F is 15 - conversion
                         */
                        decimalToBinary(ch - 'a' + 10);
                } else if (ch >= 'A' && ch <= 'F') {
                        /*
                         * input character is any value from A to F
                         * A is 10, B is 11.. F is 15 - conversion
                         */
                        decimalToBinary(ch - 'A' + 10);
                } else {
                        /*
                         * valid hexadecimal values 0-9, a-f, A-F
                         * if input doesn't belong to any of the
                         * above values, then its a wrong input
                         */
                        printf("Wrong Input!!!\n");
                        return (-1);
                }
        }
        printf("Equivalent Decimal Value: %s\n", output);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out 
  Enter your hexadecimal value:5f5
  Equivalent Decimal Value: 010111110101



No comments:

Post a Comment