Octal to Hexadecimal

Hi guys!
I am trying to find a way to convert directly from octal to hex.
I found a lot of methods around the internet, but i didn't understand
how and why i make a mistake trying to convert the number.
I found out that from hex to octal you divide by 20.
So number 173(hex) converts like this to octal:
173:20 = 8.65 >> here 8 is the first remainder
0.65 * 20 = 13 >> i know in hex is B from octal, not decimal.
Now i make the number in the calculator and it shows 7B, not
like mine which is 8B. Can someone explain where i am wrong?
I don't understand :(
closed account (o1vk4iN6)
Well depending on how your programming it you could just do something as simple as this for conversion:

1
2
3
4
5
int value = 371;

cout << dec << value << endl; // decimal
cout << hex << value << endl; // hexidecimal
cout << oct << value << endl; // octal 


You could also potentially use a stringstream or some C libraries ( such as sprintf ) to create a c-string buffer to display in a label or something. Really it's just a matter of how you input and output the data, in the end it's the same for the computer.

Conversions such as these aren't simple one number multiplications (not any octal number * 20), since each has a different base number you need to multiple according to that as well as the decimal position.

So if you have 070 octal to decimal, that's 7 * 8 which is 56.

0700 octal to decimal, 7 * 8 * 8 which is 488.
Last edited on
Just to check your numbers, you could try the following:
// modify basefield
#include <iostream>
using namespace std;

int main () {
int n;
n=371; // 173(hex) in decimal
cout << "371 in octal is " << oct << showbase << n << endl;
cout << "371 in decimal is " << dec << showbase << n << endl;
cout << "371 in hex is " << hex << showbase << n << endl;
return 0;
}
I know that hex, oct and dec
This is why i want to make it myself with math.
Is there a mathematical way to convert from oct to hex?
And make the program terribly faster.
I made the same program with all 3 kinds
you had to choose which one.
I don't need the c++ way
I need the math way if there is one
I have spent a lot of time on this,
but still i can't find it.
There must be!
{Thanks for replies}
Last edited on
First make yourself clear. Do you want to conver OCTAL to HEX or HEX to OCTAL????
The method you have written above (Dividing by 20 repeatedly) is for converting OCTAL to HEX.

Next, you are supposed to divide 173 0ctal by 20 Octal. But you are dividing 173 dec by 20 dec.
173 Octal/ 20 Octal = 123 dec / 16 dec = 7 dec remainder 11 dec = 7 oct remainder 13 oct

So basically, this method assumes that, either you know division of Octal by Octal or you know methods of converting Octal to Decimal or Vice versa. If you don't know either of these, then either learn them or discard this method.
> I need the math way if there is one

See: http://www.deimel.org/comp_sci/conversion.htm
Last edited on
Now about the solution.

Octal to Hex conversion can be done by several different ways, but I only know the indirect ones, meaning that you first convert Octal to some other base (either binary or Decimal) and then you convert that to Hex.

So your first option will be to convert the Octal to decimal (which is straight forward, simply multiply each digit to its equivalent weight) and then to hex (By repeated division by 16)

But since Octal and Hex are both even multiples of two, there exists a more easy and sophisticated method to convert one to other. Eg: for converting 452 Octal to 12A Hex, you can proceed as follows, convert each Octal digit to its equivalent three digit binary, regroup the string into 4 digit binary, convert directly to Hex.

452 octal = 000 100 101 010 (Three extra zeroes added to make grouping into 4 easier)
Now regrouping them into 4 digits

0001 0010 1010 = 12A HEX

Now all you need to do is to write a (not so) simple program to do the conversion. Go with the first method, it will be easier.

source : http://wiki.answers.com/Q/How_do_you_convert_Hexadecimal_to_octal
Topic archived. No new replies allowed.