Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

There has been a similar question before: How to convert a hexadecimal number to an octal number?

But, in my case I need an Algorithm to directly convert a number from Octal to Hexadecimal and back without converting it to binary/decimal as an intermediate step. Is it possible?

share|cite|improve this question
Use octal digits as hexadecimal ones and then just add the numbers you got with weights of $8^n$, working in hexadecimal. – Ruslan Jan 15 '14 at 13:59
Could you give a more thorough explanation, or give an example? – Ranveer Jan 15 '14 at 14:03
Take a pit stop in binary. – Gaffney Jan 15 '14 at 14:11
If you have a lot of memory you could note that since an octal number can store 3 bits per digit and an hex number can store 4. Then you could have a big lookup table. Since for a group of 4 octal digits there will be 3 hex ones. But I'd go via binary – Warren Hill Jan 15 '14 at 14:12
@Warren Hill: that is only $8^4=2^{12}=4096$ entries, not a lot of memory. – Ross Millikan Jan 15 '14 at 14:21
up vote 3 down vote accepted

Use octal digits as hexadecimal ones and then just add the numbers you got with weights of $8^n$, working in hexadecimal.

For example: consider $a=347_8$. To get a hexadecimal representation without resorting to binary or decimal, you can use the fact that $8<16$, i.e. just take the digits as they are. Now you just do the computation:

$$\text{hex}(a)=7\cdot 8^0+4\cdot 8^1+3\cdot 8^2=7_{16}+20_{16}+\text{C}0_{16}=\text{E}7_{16}.$$

Keep in mind that you have to do the multiplication and addition in hexadecimal to fullfill your requirements to not leave hex/oct representation.

share|cite|improve this answer
Looks good! What about the reverse conversion? Is there a fast method there too? – Ranveer Jan 15 '14 at 18:11
@RVA I think there's nothing much better than successive finding remainders of division by 8. – Ruslan Jan 15 '14 at 19:42
won't your method work in this case too? I mean taking the digits as they are and then multiplication by 16 and conversion to oct. And what's the logic behind 8<16? – Ranveer Jan 16 '14 at 7:41
As $8<16$, all the digits in octal are digits in hexadecimal, where there're also other digits. This is why you can just take them as they are and reinterpret as hexadecimal ones. With conversion back you can't take, e.g. $9_{16}$ and interpret it as an octal digit - you need first to convert it itself to $11_8$. After that, of course, you can just use the same method (with your new exponent base being $10_{16}=20_8$). – Ruslan Jan 16 '14 at 11:21
Right! Thanks a lot! – Ranveer Jan 16 '14 at 12:03

Using binary representation as an intermediate step really saves you a lot of computation. Consider $347_8$, you can convert it to binary just digit by digit. $$347_8 = 11\, 100\, 111_2$$ Now you regroup the bits into groups of $4$ and go the other way to hexadecimal. $$\rm 1110\,0111_2 = E7_{16}$$ No need to do any computation except from converting single digits at all.

share|cite|improve this answer
The question says "without converting it to binary/decimal as an intermediate step" – Ruslan Jan 15 '14 at 14:19
I know, that's why I made a strong argument why that is a bad idea. – Christoph Jan 15 '14 at 14:19

group the octal digits in groups of 4: $\{O_1,O_2,O_3,O_4\}$ prepending $0$ as needed

then the first hex digit is $O_1*2+\lfloor \frac{O_2}{4}\rfloor$

the second is $O_2*4+\lfloor \frac{O_3}{2}\rfloor \mod 16$

then the last digit is $O_3*8+O_4 \mod 16$

I make use of the fact that $8^4=16^3$ so each group of 4 octal digits maps 1-1 to 3 hex digits.

to use the example of $347_8$ from the other answers my digits are $\{0,3,4,7\}$

the first hex digit is $0*2+\lfloor \frac{3}{4}\rfloor=0$

the second is $3*4+\lfloor \frac{4}{2}\rfloor \mod 16=12+2=14=E_{16}$

the third digit is $4*8+7 \mod 16=32+7\mod16=7$

so the result is $\text{0E7}_{16}$

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.