【问题标题】:How to make a hex to octal converter in C [closed]如何在 C 中制作十六进制到八进制转换器 [关闭]
【发布时间】:2016-04-15 05:59:23
【问题描述】:

我希望在 C 中将十六进制转换为八进制,现在我可以将字符串从十六进制转换为二进制。现在我正在寻找将它从二进制转换为八进制。我相信这是最简单的方法。 这就是我现在所拥有的。它是在主程序中调用的函数。 从现在开始我该怎么走?我有点卡住了,任何帮助都会很棒。 提前致谢。

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




}

【问题讨论】:

  • 不需要先转换成二进制。每个十六进制数字都有一个精确的八进制值。因此,您可以完全按照您对二进制所做的操作,但将其替换为十六进制到八进制的映射(例如,F 十六进制是 17 八进制)。更好的是,使用查找表。
  • @kaylum F 并不意味着 17 如果它不在 3 的倍数位置。所以不,事情没那么简单。 (但是,您可以将 3 个十六进制数字组转换为 4 个八进制数字组)
  • @immibis 我对 OP 问题的解读是它应该转换一个十六进制数字。至少这是当前代码正在做的事情。也许我理解错了。
  • @immibis 我该怎么做?
  • 这里有一个链接可以帮助你Hex to Octal conversion in C

标签: c converter number-systems


【解决方案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;
}

【讨论】:

    【解决方案2】:

    您可以使用两种方式将十六进制转换为八进制:

    方式 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);
    
     }
    

    【讨论】:

    • 感谢您的回答。我如何将字符串分成不同的部分?有这样做的功能吗?除此之外,这是很多帮助非常感谢!
    • 我不确定任何函数,但你可以写一个函数。我认为功能会很简单。首先从二进制字符串的最后一个字符开始。尝试从二进制字符串到临时字符串从右到左取 3 个字符,然后使用比较,即 if else 条件,然后形成一个八进制数。如果仍然有任何混淆,请评论它:)
    • 非常感谢,我会试一试!
    • 我很难在字符串中循环 :( 我已经在代码底部添加了我现在尝试过的内容
    • 我添加了一个将二进制字符串转换为八进制数的代码。就试一试吧。并根据要求将上面的代码分成一个函数,因为我已经在main中编写了所有代码。如果您仍然无法获得代码评论:)顺便说一句,感谢您接受我的解决方案:)
    猜你喜欢
    • 2019-10-27
    • 2021-01-29
    • 1970-01-01
    • 2017-06-04
    • 2018-01-05
    • 2013-11-07
    • 2011-08-21
    • 2015-12-12
    • 1970-01-01
    相关资源
    最近更新 更多