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 2 of 2

Thread: decimal to hex

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 6 Times in 5 Posts

    Default decimal to hex

    i just need to be able to convert a (maximum) 3 digit decimal number to hex

    Is this the best way to do it, or is there a simple way that i dont see?
    (excluding any built in hex functionality for java)

    public String fullConversion(int n)
    {
    String result = "";
    result += convertSingleNumber(n/256);
    n = n%256;
    result += convertSingleNumber(n/16);
    n = n%16;
    result += convertSingleNumber(n);
    return result;
    }
     
    private String convertSingleNumber(int i)
    {
    String hex="";
    if(i > 9)
    {
    hex += (char)('A'+((i%10)));
    }
    else
    hex += i;
    return hex;
    }


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

    Default Re: decimal to hex

    That probably is the best way to do it without any built in functions

Similar Threads

  1. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Programming Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 03:08 PM
  2. [SOLVED] How to use decimal place when formatting an output?
    By napenthia in forum Java Theory & Questions
    Replies: 2
    Last Post: April 27th, 2009, 03:17 AM