Search code examples
cconvertersnumber-systems

How to make a hex to octal converter in C


I'm looking to make a hex to octal convert in C, and right now I'm able to convert a string from hex to binary. Now I'm looking to convert it from binary to octal. I believe that this was the simplest way. This is what I have for now. It's a function being called in a main program. How do I go from now? I'm kind of stuck and any help would be great. Thanks in advance.

#include "my_lib_3.h"
#include <string.h>
#include "mrb_lib_1.h"
#include <math.h>

char *hex2octal(char s[]){    
char input_string [20] = "";
//char return_string[50] = "";
int  num, binary_val, decimal_val = 0, base = 1, rem;
printf("Enter a hex number:\n");
scanf("%s",input_string);
int i;
for (i=0; i<1; i++) { 
switch(input_string[i]){
case '0' :
//    strcat(return_string, "0000");
 num = "0000";
break;
case '1' :
//    strcat(return_string, "0001");
 num = "0001";
break;
case '2' :
//    strcat(return_string, "0010");
 num = "0010";
break;
case '3':
//    strcat(return_string, "0011");
 num = "0011";
break;
case '4':
//    strcat(return_string, "0100");
 num = "0100";
break;
case '5':
//    strcat(return_string, "0101");
 num = "0101";
break;          
case '6':
//    strcat(return_string, "0110");
 num = "0110";
break;
case '7':
//    strcat(return_string, "0111");
 num = "0111";
break;
case '8':
//    strcat(return_string, "1000");
 num = "1000";
break;
case '9':
//    strcat(return_string, "1001");
 num = "1001";
break;
case 'A':
//    strcat(return_string, "1010");
 num = "1010";
break;
case 'B':
//    strcat(return_string, "1011");
 num = "1011";
break;
case 'C':
//    strcat(return_string, "1100");
 num = "1100";
break;
case 'D':
//    strcat(return_string, "1101");
 num = "1101";
break;
case 'E':
//    strcat(return_string, "1110");
 num = "1110";
break;
case 'F':
//  strcat(return_string, "1111");
 num = "1111";
break;  
default:
printf("Program doesn't support this yet\n");   
break;


}

printf("The binary equivalent of %s is %s\n", input_string, num);


int z; 
for (z = 0; return_string[z] != '\0'; z++) {

    if(return_string[z] = '5'){
        printf("%i",z);
        printf("Yo!\n");
        z++;
    }
}
return 0;



}




char *octal2dec(char s[]){




}

Solution

  • You can convert HEX to Octal by using two ways :

    Way 1 - Convert number into binary then convert binary into Octal

    Way 2 - As I can see that your binary is in string ,

    so do it like this :

    1-Take a group of 3 binary code from right to left

    2-Convert it into Octal

    For example you have hex number as F5

    now binary of this number is 1111 0101

    to convert it in octal make a group of 3 from binary number as

    11 110 101

    and then convert each group into octal as

    binary -- octal

    101 -- 5

    110 -- 6

    011 -- 3

    Therefore octal representation of your HEX number F5 is 365

    Code to convert binary string to octal representation

    int main{
    
    char str[]="11110101";  //binary string 
    char temp[4];    //temp string
    int j,i,length,flag=0,oct=0,num=0,t;
    length=strlen(str); //length of the binary string 
    //printf("%d",length);
    
     i=length-1;  //last index of a binary string 
    
     while(i>=0){
    
        j=2;    // as we want to divide it into grp of 3
        while(j>=0){
    
            if(i>=0){
    
            temp[j]=str[i--];  // take 3 characters in the temporary string 
            j--;
          }
          else{
            flag=1;   // if binary string length is not numtiple of 3
            break;
          }
    
        }
        if(flag==1){
    
        while(j>=0){
            temp[j]='0';  //add leading 0 if length is not multiple of 3 
            j--;
        }
         flag=0;
        }
    
        temp[3]='\0'; //add null character at the end of the tempory string 
    
    
    
        // use comparisons of tempory string with binary numbers 
        if(strcmp(temp,"000")==0){
            oct=oct*10+0;
        }
        else if(strcmp(temp,"001")==0){
            oct=oct*10+1;
        }
        else if(strcmp(temp,"010")==0){
            oct=oct*10+2;
        }
        else if(strcmp(temp,"011")==0){
            oct=oct*10+3;
        }
        else if(strcmp(temp,"100")==0){
            oct=oct*10+4;
        }
        else if(strcmp(temp,"101")==0){
            oct=oct*10+5;
        }
        else if(strcmp(temp,"110")==0){
            oct=oct*10+6;
        }
        else if(strcmp(temp,"111")==0){
            oct=oct*10+7;
        }
    
       //        printf("\n%s",temp);
    
     }
    
     //as we move from last first character reverse the number
     num=oct;
     flag=0;
     t=1;
     while(num>0){
        if(flag==0){
    
          t=num%10;
          flag=1;
         }
        else{
            t=t*10+num%10;
        }
         num=num/10;
     }
    
    
    
     //print the octal number
     printf("\n Octal number is : %d",t);
    
     }