This blog is under construction

Friday 5 July 2013

C program to convert hexadecimal to octal

How to convert hexadecimal value to octal equivalent?
Hexadecimal Value 0x1EC
1EC = (1) (1110) (1100) = (111) (101) (100) = 754
  • Find binary value for each digit and append the outputs.
  • Group the binary values(3 digits in each group).
  • Find the octal value for each group and append the outputs.

Write a C program to convert hexadecimal value to octal.


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

  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', '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);
  }

  void binaryToOctal(char *data) {
        int i = 0, res = 0, val = 0, count = 2;

        printf("Equivalent Octal Value: ");
        /*
         * find octal value for every 3 digit
         * (start from MSB) and print it
         */
        while (data[i] != '\0') {
                val = val + (data[i] - '0') * pow(2, count);
                i++;
                count--;
                if (i % 3 == 0) {
                        printf("%d", val);
                        val = 0, count = 2;
                }
        }
        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 Hexadecimal value: 0x");
        fgets(input, 10, stdin);
        input[strlen(input) - 1] = '\0';

        memset(result, 0, 256);
        /* convert hexadecimal value to binary first */
        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') {
                        decimalToBinary(ch-'A' + 10);
                } else if (ch >= 'a' && ch <= 'f') {
                        decimalToBinary(ch-'a'+ 10);
                } else {
                        /*
                         * valid octal values 0-9/a-f/A-F, 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 3 digit alignment for the above obtained binary
         * output.  For example, if the binary output is 1, make
         * it three digit aligned.  001
         */
        if (len % 3 != 0) {
                for (i = strlen(result); i >= 0; i--) {
                        result[i + (3 - (len % 3))] = result[i];
                }
                len = 3 - (len % 3);
                while (len > 0) {
                        result[len - 1] = '0';
                        len--;
                }
        }
        /* converting binary to octal value */
        binaryToOctal(result);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your Hexadecimal value: 0x9a532b
  Equivalent Octal Value: 46451453



No comments:

Post a Comment