Results 1 to 5 of 5
Thread: Hex To Octal
- 11-21-2010, 02:49 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 37
- Rep Power
- 0
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 #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
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 #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 11-21-2010, 08:16 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 37
- Rep Power
- 0
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 #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
If you can read/process the characters in a String front to back you can also read them backwards; compare:
Java Code:for (int i= 0; i < s.length(); i++) // process the charAt(i);
Java Code:for (int i= s.length(); i-- > 0;) // process the charAt(i);
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads
-
How to convert integer to the hexadecimal and octal number
By Java Tip in forum java.langReplies: 0Last Post: 04-06-2008, 08:40 PM
Bookmarks