我正在寻找在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[]){
}
答案 0 :(得分: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;
}
答案 1 :(得分:0)
您可以通过两种方式将HEX转换为八进制:
路 1 - 将数字转换为二进制,然后将二进制转换为八进制
方式2 - 我可以看到你的二进制文件是字符串,
所以这样做:
1 - 从从右到左
中取一组3个二进制代码2 - 将其转换为Octal
例如您的十六进制数字为 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);
}