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




}
4

2 回答 2

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;
}
于 2016-01-11T02:50:29.427 回答
0

您可以使用两种方式将 HEX 转换为 Octal:

方式 1 - 将数字转换为二进制,然后将二进制转换为八进制

方式 2 - 我可以看到你的二进制文件是 string ,

所以这样做:

1-从右到左取一组3个二进制码

2-将其转换为八进制

例如,您的十六进制数为F5

现在这个数字的二进制是 1111 0101

将其转换为八进制,将二进制数中的一组 3 设为

11 110 101

然后将每个组转换为八进制为

二进制 -- 八进制

101 -- 5

110 -- 6

011 -- 3

因此,您的十六进制数F5的八进制表示是 365

将二进制字符串转换为八进制表示的代码

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

 }
于 2016-01-11T02:16:08.313 回答