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?
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
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 hexadecimal: 1 F 3
You need to group 4 octal digits to get 3 hexa digits.
If you have 1 or 2 octal digits, it's very simple:
- One single octal digit converts directly into the same hexadecimal digit. e.g.
3 -> 0x3
- if the first digit is even divide it by 2 and take the second digit as it is: 63 -> 0x33
- 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
With a third and a fourth digit, the maths is to painful, and IMHO the quickest way is to transpose the octal into binary, regroup by 4 bits and transpose in hexa.