# Hexadecimal, conversion between binary and octal

## problem

Problem description
Given n hexadecimal positive integers, output their corresponding octal numbers.

Input format
The first line of input is a positive integer n (1 < = n < = 10).
In the next n lines, each line is a string composed of 09 and uppercase letter AF, indicating the hexadecimal positive integer to be converted, and the length of each hexadecimal number shall not exceed 100000.

Output format
Output n lines, and input the corresponding octal positive integer for each line.

[note]
The entered hexadecimal number will not have a leading 0, such as 012A.
The output octal number cannot have a leading 0.

sample input
2
39
123ABC

sample output
71
4435274

[prompt]
First convert a hexadecimal number to a hexadecimal number, and then convert a hexadecimal number to an octal number.

The idea is quite obvious. First convert the hexadecimal characters one by one into 4-bit binary, and then convert the binary into octal in 3-bit units. I feel very simple, but when I actually do it, I find that it is not the case. Many details need to be paid attention to. I'm ashamed that it took me more than half a day to implement these two functions, At first, I just wanted to do this problem. Later, I thought it might be used in the future, so I considered more parameter input to make it more compatible.

The program compiled and ran successfully in C + + environment, mainly because the return value of the function is set to bool type. After being modified to int, it should be compiled in C language environment, but I didn't try it

## Converts a hexadecimal string to a binary string

For parameters:
S is the first address of the hexadecimal string and slength is the length of the string to be converted. Why not use strlen(S) directly? Considering that only part of a hexadecimal string needs to be converted to binary, an additional length of the string is specified.
B is the address where the binary string is stored, and blength is the length that B can use. For hexadecimal strings, except for the first bit, each other must be converted to four binary characters. Therefore, the ideal length of blength should be greater than slength*4. In case, B should initialize before passing in.

```//Hexadecimal string to binary string
bool StoB(char *S,int slength,char *B,int blength)
{
//Judge illegal input and return false
if((S == NULL) || (B == NULL) || (blength < slength * 4))return false;
//If the first bit of hexadecimal is less than 8, the first bit will be 0 after being converted to four bit binary, so special processing is required
char k1 = {
"0"   , "1"   , "10"  , "11"  ,
"100" , "101" , "110" , "111" ,
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111"
};
char k  = {
"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111"
};

//tmp stores the decimal value of a single hexadecimal character
//position binary flag bit
int i=0,tmp=0,position=0;
//First zero flag
bool zero = true;
for(;i<slength;i++)
{
if((*(S+i) >= '0') && (*(S+i) <= '9'))
tmp = *(S+i) - '0';
else if((*(S+i) >= 'A') && (*(S+i) <= 'Z'))
tmp = *(S+i) - 'A' + 10;
else if((*(S+i) >= 'a') && (*(S+i) <= 'z'))
tmp = *(S+i) - 'a' + 10;
else
return false;

if(tmp != 0) zero = false;
if(zero == true) continue;//Skip if first zero

if(position == 0)
{
strcpy(B+position,k1[tmp]);
position = position + strlen(k1[tmp]);
}
else
{
strcpy(B+position,k[tmp]);
position = position + 4;
}
}
return true;
}
```

## Converts a binary string to an octal string

```//Binary string to octal string
bool BtoE(char *B,int blength,char *E,int elength)
{
if((B == NULL) || (E == NULL) || (elength < (blength+3)/2)) return false;

char k  = { "000", "001", "010", "011", "100", "101", "110", "111" };
char k1 = { "0", "1", "10", "11", "100", "101", "110", "111" };

int i = 0,position = blength/3 - 1;
if((blength%3) != 0) position++;

char tmp;

for(i = blength - 3;i >= 0;i = i-3)
{
memset(tmp,0,sizeof(tmp));

strncpy(tmp,B+i,3);
for(int j = 0;j < 8;j++)
{
if(strcmp(tmp,k[j]) == 0)
{
E[position--] = j + '0';
}
}
}
if(i  > -3)
{
memset(tmp,0,sizeof(tmp));
strncpy(tmp,B,3+i);
for(int j = 0;j < 8;j++)
{
if(strcmp(tmp,k1[j]) == 0)

E[position--] = j + '0';
}
}
}

return true;
}
```

## Operation effect diagram

The specific code of the operation is the two functions above are called in the main function, and I will not paste them out if I avoid duplication.
Look at the results
Output analysis: the first line is the original input, the second line is the binary string converted, and the third line is the octal string converted

It can be seen that the ideal results can be output continuously in uppercase characters, lowercase characters, or multiple zeros For illegal input, letters out of range and irrelevant symbols, the StoB function will return false when illegal characters are judged Then try extra long characters!!!!! Longer!!!!! But now we can't judge whether the output result is correct by the naked eye, but at least it doesn't report an error, does it?

## end

Above, the introduction of hexadecimal to binary and binary to octal is over. In fact, after understanding and completing hexadecimal to binary, binary to hexadecimal should be taken for granted. One hexadecimal character is converted into binary. Similarly, four binary characters are converted into one hexadecimal character, The same is true for octal to binary.
It is also possible to convert octal to binary from hexadecimal to binary. A hexadecimal to four binary characters is the same as an octal to three binary characters. Maybe it is easier to understand?

In addition, you can find that all my paragraphs are not empty in the front. It's not that I don't want to, but I don't know how to do it for a long time

## For title

My original title was conversion from hexadecimal string to binary string and conversion from binary string to octal string. This title is very accurate, because I really only realized the conversion from hexadecimal string to binary string and binary string to octal string in my article, but at the end, I think when I understand these two, You should be able to convert between hexadecimal, octal and binary freely, so I modified the title and didn't know whether it was reasonable.

Tags: C C++

Posted on Sat, 23 Oct 2021 08:08:02 -0400 by Shaun