# Hex To Octal

• 11-21-2010, 02:49 AM
rizowski
Hex To Octal
I am currently making a program that converts a hexadecimal number to an octal number. I have it so it can recognize letters out of the sequence, but what would be the best way of getting it to identify an int out of the sequence of letters and numbers?

example of Hex: A5F6C
example of Octal: 07453

I was thinking an if statement but at the same time on my basic knowledge
if (char he7 == int)
does not work

note: he7 would be a char placement of 7 on a hex number

Thanks
• 11-21-2010, 03:35 AM
pbrockway2
Quote:

what would be the best way of getting it to identify an int out of the sequence of letters and numbers

There are Integer methods that you can use. If you know the base of the string use parseInt(string,radix).

If you don't know the base of the string you might have to look at it in terms of how it is specified. (A common convention is for octal strings to start with a zero character, any string containing 'A' to 'F' might be considered hexidecimal.) Or you can use the decode() method which assumes conventions that are detailed at that link.
• 11-21-2010, 11:10 AM
JosAH
Quote:

Originally Posted by pbrockway2
If you don't know the base of the string you might have to look at it in terms of how it is specified. (A common convention is for octal strings to start with a zero character, any string containing 'A' to 'F' might be considered hexidecimal.) Or you can use the decode() method which assumes conventions that are detailed at that link.

Man, do I hate that decode() method: in an old lexical analyzer of mine I used it and I didn't read about the #<hex digits> part while my little language uses the # sign as a line comment character ... it took me hours to find out what was really happening. My later analzyzer does all the decoding itself, so there ;-)

kind regards,

Jos
• 11-21-2010, 08:16 PM
rizowski
The only problem with this is that I am looking for a way to solve this with out using any decoders, mainly for my benefit of understanding how it works. Later on after understanding the basics i may move on to using things that already do it for me :D

But anyways after looking over my code i found a list of if statements and *16 will actually work, My next question would be is there some sort of method that flips a string or in my case chars around

For example:
Sample
elpmaS

I have actually done my math backwards and using a switch statement. I believe I need to flip the way the program reads my Hex digits in order for my math to be correct.

Edit: I believe I have seen a similar question here before but i can't find it and don't know the name of the method. sorry :/
Thanks :D
• 11-21-2010, 08:26 PM
JosAH
Quote:

Originally Posted by rizowski
I have actually done my math backwards and using a switch statement. I believe I need to flip the way the program reads my Hex digits in order for my math to be correct.

If you can read/process the characters in a String front to back you can also read them backwards; compare:

Code:

```for (int i= 0; i < s.length(); i++)   // process the charAt(i);```
and this:

Code:

```for (int i= s.length(); i-- > 0;)   // process the charAt(i);```
kind regards,

Jos