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

Thread: Decimal to Hexadecimal, Without Java Libraries.

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Decimal to Hexadecimal, Without Java Libraries.

    Hello, so I have to create a program in java that does not use any automatic converting libraries. I've completed decimal to binary, and now im stuck on decimal to hexadecimal. The code i've wrote for this is below. Any comments would be greatly appreciated.

    public void getDec()
        {
          String Dec;
          Dec = JOptionPane.showInputDialog("Please enter the decimal number you would like to convert");
          num = Integer.parseInt(Dec);
        }
     
     
     
       public void decToHex()
        {
            getDec();               //ask user for a decimal 
            int hex[] = new int[8]; //container for new hexadecimal
            int i, j = 0;              
     
            while (num != 0)        //base 16 code
            {
                num = num / 16;
                hex[i]=num;
                hex[i] = hex[i] % 16;
                i++;
            }
            while(hex[j]>=10)   
            {
              switch(hex[j])
              {
               case 10: hex[j] = 'A';
                     j--;
                     break;
               case 11: hex[j] = 'B';
                     j--;
                     break;
               case 12: hex[j] = 'C';
                      j--;
                     break;
               case 13: hex[j] = 'D';
                     j--;
                     break;
               case 14: hex[j] = 'E';
                     j--;
                     break;
               case 15: hex[j] = 'F';
                     j--;
                     break;
              }
            }
            //System.out.println(?);
           }
    Last edited by ribmant; February 2nd, 2012 at 08:12 PM.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Decimal to Hexadecimal, Without Java Libraries.

    Are there any problems with the code?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Decimal to Hexadecimal, Without Java Libraries.

    Instead of a switch statement you could use an array.
    Or consider that chars can be used in arithmetic statements:
    int value = 'A' + (nbr - 10);

Similar Threads

  1. New to Java Need 2 decimal places, please :). Here is my code
    By Charyl in forum Member Introductions
    Replies: 2
    Last Post: June 28th, 2011, 03:06 AM
  2. The difference between two Java libraries
    By ice in forum Java Theory & Questions
    Replies: 1
    Last Post: January 11th, 2011, 10:34 AM
  3. How to convert a String into an Hexadecimal ?
    By lumpy in forum Java Theory & Questions
    Replies: 2
    Last Post: February 16th, 2010, 05:01 PM
  4. Hexadecimal answer rather than decimal
    By Eric in forum Java Theory & Questions
    Replies: 1
    Last Post: June 22nd, 2009, 11:36 AM
  5. Program to convert Hexadecimal to its Character equivalent
    By nathanernest in forum Java Theory & Questions
    Replies: 2
    Last Post: April 8th, 2009, 03:12 AM

Tags for this Thread