Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: numerical conversion methods..

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default numerical conversion methods..

    i was looking for any methods in Math package that can convert any number such as [DECIMAL-OCTAL],
    [BINARY-OCTAL], [HEXA-OCTAL], [BINARY-HEXA], ETC.. (my proffesor told me,) but i cant find any of it,

    are there any methods such like that? or should i define my own class to perform this tasks?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: numerical conversion methods..

    Technically, it only matters on displaying them, or in the actual code, since when the program is running, everything is in binary. To enter a hex number add the 0x to the front of the number. Unfortunately, I don't know what to put to enter in as binary or octal, but I do know decimal has no prefix

    In the printf function, I believe if you do "%0xd", it should print out the value as a hex, but i'm not positive.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (September 24th, 2009)

  4. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: numerical conversion methods..

    How about this:

            System.out.println(Integer.toHexString(195012));
            System.out.println(Integer.toBinaryString(195012));
            System.out.println(Integer.toOctalString(195012));

    Expected output would be:

    2f9c4
    101111100111000100
    574704
    // Json

  5. The Following User Says Thank You to Json For This Useful Post:

    chronoz13 (September 24th, 2009)

  6. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: numerical conversion methods..

    Damn! thats it!!! that's what i've been looking for! tnx sir JSon!!!! your really awesome!
    i never knew that those methods were in Integer class..I thought it was in Math. Im using netbeans but i didnt notice all of those methods when everytime im using that class

  7. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: numerical conversion methods..

    my question is, regarding with OCTAL conversion.. why is it converted into String?
    (i understand a liitle bit with hexa and binary) ..

    and how could i reverse those methods?

    if i input
    10001110011
    and i want it to convert into a Decimal? or
    if i input
    CABACABACABA
    and i want it to convert into a Decimal?


    hmm.

  8. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: numerical conversion methods..

    You'll have some trouble converting the second number to an integer... it's longer than 4 bytes

    Binary String to Decimal int:

    public static int binaryToDecimal(String num)
    {
         int sum = 0;
         for (int i = 0; i <num.length(); i++)
         {
              sum += Integer.parseInt(""+num.charAt(i))*Math.pow(2,num.length()-1-i);
         }
         return sum;
    }

    Hex works basically the same way, except you have to figure out how to get the value of the number at each digit differently because parseInt doesn't recognize A-F as 10-15

  9. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (September 25th, 2009)

  10. #7
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: numerical conversion methods..

    Ok, let me extend my first example.

            System.out.println("195012 hex: " + Integer.toHexString(195012));
            System.out.println("195012 binary: " + Integer.toBinaryString(195012));
            System.out.println("195012 octal: " + Integer.toOctalString(195012));
            System.out.println("From hex: " + Integer.decode("#2f9c4"));
            System.out.println("From binary: " + Integer.parseInt("101111100111000100", 2));
            System.out.println("From octal: " + Integer.decode("0574704"));

    Output:

    195012 hex: 2f9c4
    195012 binary: 101111100111000100
    195012 octal: 574704
    From hex: 195012
    From binary: 195012
    From octal: 195012
    // Json

  11. The Following User Says Thank You to Json For This Useful Post:

    chronoz13 (September 25th, 2009)

  12. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: numerical conversion methods..

    WOW TNX AGAIN SIR JSON!!

    TNX A LOT!!

  13. #9
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: numerical conversion methods..

    ahh so thats the purpose of the .decode() method of Integer Class... tnx again!!

  14. #10
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: numerical conversion methods..

    but why .parseInt in binary?

    System.out.println("From binary: " + Integer.parseInt("101111100111000100", 2));

  15. #11
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: numerical conversion methods..

    Because there does not appear to be a decode for the binary number.

    // Json

  16. #12
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: numerical conversion methods..

    You could also have used parseInt("12345670",8) for octal numbers.

  17. #13
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: numerical conversion methods..

    Indeed

    // Json

Similar Threads

  1. binary conversion..
    By chronoz13 in forum Java Theory & Questions
    Replies: 8
    Last Post: September 16th, 2009, 10:47 AM