# Number system conversion

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 2 '18 at 6:38
• Who 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 2 '18 at 8:21

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
``````
• I like your answer. With a nice sauce, potatoes and a good red. – Bent Oct 4 '18 at 15:01

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.

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.

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.

What do you mean with directly ?

You can always directly convert a number of any base to a number of any other base.

For instance, a number made of n digits `Dn ... D1` in base B, has a numerical value of `Dn*B^(n-1) + ...+ D0*B^0`. You can then convert this to a number of base C with a succession of modulo and integer division.

Did you mean trivial conversion, without calculation ?

Then it cannot be made directly. There's no simple transposition, because octal is base 8 = 2^3 so each digit is represented in 3 bits, and hexadecimal is base 16 = 2^4 so each digit corresponds to 4 bits. So the bits in the binary representation of the number are grouped differently.

Let's take the example

``````octal:                               7   6   3
octal grouping, group of 3:        111 110 011
hexa grouping, group of 4:     0001 1111  0011
• One single octal digit converts directly into the same hexadecimal digit. e.g. `3 -> 0x3`
• if the first digit is odd, substract 1 and divide by 2, but add 8 to the second digit and for the value between 10 and 15 replace it with A to F: `73 -> 3 8+3 -> 3 11 -> 0x3b`