Why can't we directly convert octal number to base 8 to hexadecimal base 16 directly ? We can convert to octal by first converting it into binary equivalent, or by converting it to decimal equivalent . But why can't we directly convert base 8 octal to base 16 hexadecimal?
-
Generally, it is just preferable to "interpret" the octal number in its binary form and then write it to hexadecimal. Something which converts directly from octal to hexadecimal is going to be completely useless when you must convert from some other starting base, whereas if you had a hexadecimal serializer, you've already done half the job. – Neil Oct 02 '18 at 06:38
-
7Who says that "we can't do that"? Converting from octal to hexadecimal works exactly the same as converting from octal to decimal or from octal to binary or from nonal to sexagesimal or from any base to any base. Why would that exact conversion not work, when *every other possible conversion* does? – Jörg W Mittag Oct 02 '18 at 08:21
4 Answers
You can, by successively dividing by 020, same as you do in decimal. Say, you have 0137357 (omitting the prefix for brevity from this point):
137357 / 20 = 5756 remainder 17 = 0xf
5756 / 20 = 276 remainder 16 = 0xe
276 / 20 = 13 remainder 16 = 0xe
13 < 20, so highest digit is 0xb
0xbeef

- 146,727
- 38
- 279
- 479
You can do this conversion directly — but realize that octal, hexadecimal, and decimal are properties of strings of characters when their digit sequences are interpreted as numbers. So an octal representation is not an efficient why to store a number; and while hex is better, it is still far from being as efficient as the native number storage mechanism.
Internally for the CPU, the integer formats (e.g. 32-bit, 64-bit, others) numbers are just numbers. The processor will store them as numbers using binary bits, but that isn't very significant to this discussion. What is significant is the fixed size vs. variable size, and the native storage of numbers vs. their representation in strings of characters.

- 33,282
- 5
- 57
- 91
You can convert directly from octal to hexadecimal. As a brute force solution, create a 12 bit lookup table, index into that with four octal digits, get out three hexadecimal digits. Other less space intensive solutions exist.

- 22,899
- 9
- 58
- 61
Probably, you have something in mind like the following code lines (writing in Java, but will be similar in other languages):
String octalText = "1357";
int value = Integer.parseInt(text, 8);
String hexText = Integer.toString(value, 16);
And yes, it's a two-step process, it converts from a base-8 text representation "1357" of the number 751 to the machine-internal value, and then in a second step from the internal form to the base-16 text form "2ef".
For conversions between number bases, that's the standard way to do it. Of course, it's possible to write functions that do the conversion in one call:
String convertBase(String textIn, int baseIn, int baseOut)
but in the general case, converting number systems involves a lot of computation, and computation is done most efficiently in the machine-internal form, so effectively it will do something very similar to the two-step process internally.
In a few special cases, a text-form conversion can be done more efficiently than going through the machine-internal numbers, and for the base-8 to base-16 conversion, the lookup-table approach mentioned by Kendall looks promising.

- 5,891
- 15
- 19