Results 1 to 5 of 5

Thread: Hex To Octal

  1. #1
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default 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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by pbrockway2 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    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

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by rizowski View Post
    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:

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

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

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 04-06-2008, 08:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •