-2

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[]){




}
  • There is no need to convert to binary first. Each hex digit has a precise octal value. So you can do exactly what you have done for binary but replace it with the hex to octal mapping (e.g. `F` hex is `17` octal). Better still, use a lookup table. – kaylum Jan 11 '16 at 01:44
  • @kaylum `F` does not mean `17` if it's not in a multiple-of-3 position. So no, it's not that simple. (You can, however, convert groups of 3 hex digits into groups of 4 octal digits) – user253751 Jan 11 '16 at 01:44
  • @immibis My reading of the OP question is that it is supposed to convert one hex digit. At least that is what the current code is doing. Maybe I understood it wrong. – kaylum Jan 11 '16 at 01:46
  • @immibis How would I do that? – Ramsey Harrison Jan 11 '16 at 01:51
  • Here is a link that would help you [Hex to Octal conversion in C](http://www.codeforwin.in/2015/09/c-program-to-convert-hexadecimal-to-octal-number-system.html). – Pankaj Prakash Jan 11 '16 at 03:06

2 Answers2

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

char *hex2octal(const char s[]){
    size_t hlen = strlen(s);
    size_t olen = hlen * 4 / 3 + 1;//one hex charactor : 4bit, one oct charactor : 3bit
    //Normalization : e.g BEEF => 00BEEF
    size_t add0_len = (3 - hlen % 3) % 3;
    char *temp_hex = malloc(hlen + add0_len + 1);
    memset(temp_hex, '0', add0_len);//padding '0' to top
    memcpy(temp_hex + add0_len, s, hlen);
    hlen += add0_len;
    temp_hex[hlen] = 0;

    char three_hex[4] = {0};
    char *return_string = malloc(olen + 1);
    size_t len = 0;
    for(int i = 0; i < hlen; i += 3){
        memcpy(three_hex , temp_hex + i, 3);
        unsigned n = strtoul(three_hex, NULL, 16);
        len += sprintf(return_string + len, i ? "%04o" : "%o", n);
    }
    free(temp_hex);
    return return_string;
}

int main(void){
    char hex_str[] ="DEADBEEF";
    char *octal = hex2octal(hex_str);
    printf("%s\n", octal);//33653337357
    free(octal);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

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);

 }
Nutan
  • 778
  • 1
  • 8
  • 18
  • Thanks for answering. How would I divide the string in different parts? Is there a function to do this? Other than that this was lots of help thanks much ! – Ramsey Harrison Jan 11 '16 at 15:12
  • I am not sure about any function, but you can write a function. I think function will be quite simple. First start from last characters of your binary string. try to take 3 characters right to left from the binary string to temporary string and then use comparisons i.e if else conditions and then form a octal number. If still there is any confusion just comment it :) – Nutan Jan 11 '16 at 17:08
  • Great thanks I'll give it a shot! – Ramsey Harrison Jan 11 '16 at 17:30
  • I'm having a tough time cycling through the string :( I've added to the bottom of the code what I have tried for now – Ramsey Harrison Jan 11 '16 at 23:30
  • I have added a code to convert binary string to the octal number. Just try it. and divide above code into a function as per requirement as I have written all code in main. If still you are unable to get code comment it :) BTW thanks for accepting my solution :) – Nutan Jan 12 '16 at 04:50