This blog is under construction

Friday 5 July 2013

C program to convert octal to hexadecimal

How to convert octal to hexadecimal?
Octal Value 745
745 = (111) (100) (101) = 1 (1110) (0101) = (0001) (1110) (0101) = (1) (E) (5)
  • Find binary value for each digit and append the outputs.
  • Group the binary values(4 digits in each group).
  • Find the hexadecimal value for each group and append the outputs.

Write a C program to convert octal value to hexadecimal.


  #include <stdio.h>
  #include <string.h>
  #include <math.h>
  #define MAXBINARYDIG 3

  char result[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 3.  So, we are storing '0'
         * at each index of val during intialization.
         * val[] = {'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 result */
        strcat(result, val);
  }


  /* converts binary value to hexadecimal */
  void binaryToHexa(char *data) {
        int i = 0, res = 0, val = 0, count = 3;
        char hexaChar[] = {'A', 'B', 'C', 'D', 'E', 'F'};

        printf("Equivalent Hexadecimal Value: 0x");
        /*
         * find hexadecimal value for every 4 digit
         * (start from MSB) and print it
         */
        while (data[i] != '\0') {
                val = val + (data[i] - '0') * pow(2, count);
                i++;
                count--;
                if (i % 4 == 0) {
                        if (val > 9) {
                                printf("%c", hexaChar[val - 10]);
                        } else {
                                printf("%d", val);
                        }
                        val = 0, count = 3;
                }
        }
        printf("\n");
  }

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

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

        memset(result, 0, 256);
        /* convert octal value to decimal */
        for (i = 0; i < strlen(input); i++) {
                ch = input[i];
                if (ch >='0'&& ch <= '7') {
                        /*
                         * input character is any value from 0 to 9
                         */
                        decimalToBinary(ch-'0');
                } else {
                        /*
                         * valid octal values 0-7, if the input
                         * doesn't belong to any of the above
                         * values, then its a wrong input
                         */
                        printf("Wrong Input!!!\n");
                        return (-1);
                }
        }
        len = strlen(result);
        /* 
         * doing 4 digit alignment for the above obtained binary
         * output.  For example, if the binary output is 1, make
         * it four digit aligned.  0001
         */
        if (len % 4 != 0) {
                for (i = strlen(result); i >= 0; i--) {
                        result[i + (4 - (len % 4))] = result[i];
                }
                len = 4 - (len % 4);
                while (len > 0) {
                        result[len - 1] = '0';
                        len--;
                }
        }

        /* converting binary value to hexadecimal value */
        binaryToHexa(result);
        return 0;
  }



Note:
gcc octToHexa.c -lm => linked match library since we have used pow() function.

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your octal value:754
  Equivalent Hexadecimal Value: 0x1EC



No comments:

Post a Comment