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

Thread: decimal to hexadecimal

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default decimal to hexadecimal

    Hi, i am still practicing with my java programming, and i decided to write a code today that would convert decimal numbers to hexadecimal numbers. Unfortunately, it does not compile. The error message is "cannot find symbol - variable A", and might be the same error for the other variables B,C,D,E,F. Could you please tell me what is wrong and why?
    Here is my code:

     
    public class dec2hex
    {
        public static void main (String [] args)
        {int nbr = 99;
            char a = A;
            char b = B;
            char c = C;
            char d = D;
            char e = E;
            char f = F;
           while (nbr<999)
           {
               int x = nbr%16;
               nbr = nbr - x;
               int y = nbr/16;
               if (x==10)
               {x=(char)a;}
               if (x==11)
               {x=(char)b;}
               if (x==12)
               {x=(char)c;}
               if (x==13)
               {x=(char)d;}
               if (x==14)
               {x=(char)e;}
               if (x==15)
               {x=(char)f;}
               if (y==10)
               {y=(char)a;}
               if (y==11)
               {y=(char)b;}
               if (y==12)
               {y=(char)c;}
               if (y==13)
               {y=(char)d;}
               if (y==14)
               {y=(char)e;}
               if (y==15)
               {y=(char)f;}
            }
            System.out.println ("le résultat est");
            System.out.print (y);
            System.out.println (x);
     
        }
    }

    Thank you for your help, it will be very much appreciated


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: decimal to hexadecimal

    char a = A;

    This is attempting to assign the primitive variable a to the value of another variable A. You can define a character literal with the ' symbol.

    char a = 'A';

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

    stylelink (July 16th, 2014)

  4. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: decimal to hexadecimal

    Hi again, i did this to the code, it compiles perfectly, i added a lot of lines but basically they do the same thing over and over again. unfortunately, the code doesn't work for some values. for instance, instead of printing "2B" for the number 43, it prints 266. I have tried to fix this by multiple different ways but i cn't seem to make it work. Here is the code:

    public class dec2hex
    {
        public static void main (String [] args)
        {int nbr = 43;
            char a = 'A';
            char b = 'B';
            char c = 'C';
            char d = 'D';
            char e = 'E';
            char f = 'F';
           if (nbr<999)
           {
               int x = nbr%16;
               nbr = nbr - x;
               int y = nbr/16;
                         if (x==0)
               {
                if (y==10)
               {y=(char)a;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==11)
               {y=(char)b;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==12)
               {y=(char)c;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==13)
               {y=(char)d;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==14)
               {y=(char)e;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==15)
               {y=(char)f;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               else
               {
                   System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);
                }
            }
     
            if (x==1)
               {
                if (y==10)
               {y=(char)a;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==11)
               {y=(char)b;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==12)
               {y=(char)c;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==13)
               {y=(char)d;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==14)
               {y=(char)e;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==15)
               {y=(char)f;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               else
               {
                   System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);
                }
            }
     
            if (x==2)
               {
                if (y==10)
               {y=(char)a;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==11)
               {y=(char)b;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==12)
               {y=(char)c;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==13)
               {y=(char)d;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==14)
               {y=(char)e;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==15)
               {y=(char)f;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               else
               {
                   System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);
                }
            }
     
            if (x==3)
               {
                if (y==10)
               {y=(char)a;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==11)
               {y=(char)b;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==12)
               {y=(char)c;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==13)
               {y=(char)d;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==14)
               {y=(char)e;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==15)
               {y=(char)f;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               else
               {
                   System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);
                }
            }
     
            if (x==4)
               {
                if (y==10)
               {y=(char)a;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==11)
               {y=(char)b;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==12)
               {y=(char)c;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==13)
               {y=(char)d;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==14)
               {y=(char)e;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==15)
               {y=(char)f;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               else
               {
                   System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);
                }
            }
     
            if (x==5)
               {
                if (y==10)
               {y=(char)a;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==11)
               {y=(char)b;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==12)
               {y=(char)c;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==13)
               {y=(char)d;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==14)
               {y=(char)e;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==15)
               {y=(char)f;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               else
               {
                   System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);
                }
            }
     
            if (x==6)
               {
                if (y==10)
               {y=(char)a;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==11)
               {y=(char)b;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==12)
               {y=(char)c;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==13)
               {y=(char)d;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==14)
               {y=(char)e;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==15)
               {y=(char)f;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               else
               {
                   System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);
                }
            }
     
            if (x==7)
               {
                if (y==10)
               {y=(char)a;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==11)
               {y=(char)b;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==12)
               {y=(char)c;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==13)
               {y=(char)d;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==14)
               {y=(char)e;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==15)
               {y=(char)f;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               else
               {
                   System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);
                }
            }
     
            if (x==8)
               {
                if (y==10)
               {y=(char)a;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==11)
               {y=(char)b;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==12)
               {y=(char)c;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==13)
               {y=(char)d;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==14)
               {y=(char)e;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==15)
               {y=(char)f;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               else
               {
                   System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);
                }
            }
     
            if (x==9)
               {
                if (y==10)
               {y=(char)a;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==11)
               {y=(char)b;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==12)
               {y=(char)c;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==13)
               {y=(char)d;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==14)
               {y=(char)e;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==15)
               {y=(char)f;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               else
               {
                   System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);
                }
            }
                        if (x==10)
               {x=(char)a;
                if (y==10)
               {y=(char)a;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==11)
               {y=(char)b;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==12)
               {y=(char)c;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==13)
               {y=(char)d;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==14)
               {y=(char)e;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==15)
               {y=(char)f;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               else
               {
                   System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);
                }
            }
                        if (x==11)
               {x=(char)b;
                if (y==10)
               {y=(char)a;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==11)
               {y=(char)b;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==12)
               {y=(char)c;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==13)
               {y=(char)d;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==14)
               {y=(char)e;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==15)
               {y=(char)f;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               else
               {
                   System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);
                }
            }
                        if (x==12)
               {x=(char)c;
                if (y==10)
               {y=(char)a;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==11)
               {y=(char)b;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==12)
               {y=(char)c;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==13)
               {y=(char)d;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==14)
               {y=(char)e;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==15)
               {y=(char)f;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               else
               {
                   System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);
                }
            }
                        if (x==13)
               {x=(char)d;
                if (y==10)
               {y=(char)a;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==11)
               {y=(char)b;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==12)
               {y=(char)c;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==13)
               {y=(char)d;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==14)
               {y=(char)e;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==15)
               {y=(char)f;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               else
               {
                   System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);
                }
            }
                        if (x==14)
               {x=(char)e;
                if (y==10)
               {y=(char)a;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==11)
               {y=(char)b;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==12)
               {y=(char)c;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==13)
               {y=(char)d;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==14)
               {y=(char)e;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==15)
               {y=(char)f;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               else
               {
                   System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);
                }
            }
                        if (x==15)
               {x=(char)f;
                if (y==10)
               {y=(char)a;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==11)
               {y=(char)b;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==12)
               {y=(char)c;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==13)
               {y=(char)d;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==14)
               {y=(char)e;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               if (y==15)
               {y=(char)f;System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);}
               else
               {
                   System.out.println ("le resultat est:");
               System.out.print (y);
               System.out.print (x);
                }
            }
     
            }
            }
     
     
        }

  5. #4
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: decimal to hexadecimal

    Quote Originally Posted by "stylelink
    i added a lot of lines but basically they do the same thing over and over again
    When you find yourself doing this ... stop and think about what you are trying to accomplish. Computers are excellent at repetition and loops are a fundamental concept for all programming languages.

    What you need is pseudo-code or an algorithm. Can you write out in a step by step fashion how to convert a base 16 hexadecimal value into a base 10 decimal one?

  6. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    stylelink (July 16th, 2014)

  7. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: decimal to hexadecimal

    thanks again, i decided to do something differently, but for that, i need to be able to call a separate class for help (basically, i write a class that would assign values to the 10/11/12/13/14/15 numbers, and call it out in my main class when needed), but i have no idea how to do that.

  8. #6
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: decimal to hexadecimal

    Quote Originally Posted by stylelink View Post
    thanks again, i decided to do something differently,
    Good for you! Trying something then learning from the mistakes and trying something else is the best way to learn

    Quote Originally Posted by stylelink
    but for that, i need to be able to call a separate class for help
    Now you are on the right track! But instead of writing something from scratch why not look for something that already exists? The Integer class is a good place to start.


    (Okay.. a quick extra hint because the terminology will be difficult to a non native english speaker. The word 'radix' is a programmers term for the base of the integer.)
    Last edited by ChristopherLowe; July 16th, 2014 at 11:08 AM.

  9. #7
    Junior Member
    Join Date
    Jul 2014
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: decimal to hexadecimal

    what i was thinking is just writing a class separately, which would take a number from 10 to 15 and return its associated letter, here from A to F. But i have no idea how to call it and make it work. If the class name was "Asso", how would i make it work in my converter? would i call it like:
    " Asso(y) " for y the variable that i inputed? or another way?

    PS: My goal here is to create the converter, in the link you sent, there already is a converter, but i would rather not read it until i am finished (also i'm pretty sure it will be way cleaner/efficient than the one i am working on).
    Last edited by stylelink; July 16th, 2014 at 12:51 PM. Reason: PS

  10. #8
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: decimal to hexadecimal

    Quote Originally Posted by stylelink View Post
    what i was thinking is just writing a class separately, which would take a number from 10 to 15 and return its associated letter, here from A to F.
    The approach is faulty. Hexadecimal is just a way of expressing a number in a different base. It's incorrect to look at a character and return what that character would be in decimal. You need to an algorithm to convert bases.

    Quote Originally Posted by styelink
    But i have no idea how to call it and make it work. If the class name was "Asso", how would i make it work in my converter? would i call it like:
    " Asso(y) " for y the variable that i inputed? or another way?
    Asso(y) would be the constructor for the object. You would create a member variable and assign it to the value of y. Then you would have a method of Asso that returns the converted result.

    public class Asso {
       private String _hex;
     
       public Asso(String hexValue) {
          _hex = hexValue;  //Assign the member variable
       }
     
       public int convertToDecimal() {
             // Convert base 16 to base 10
            return result;
       }
    }

    Then when you want to do a conversion

    Asso asso = new Asso("4E2A");
    int result = asso.convertToDecimal();
     
    // Check the result against the one provided with Integer.parse 
    if (result == (Integer.parse("4E2A", 16)) {
        System.out.println("My algorithm is correct");
    }

    Quote Originally Posted by stylelink View Post
    PS: My goal here is to create the converter, in the link you sent, there already is a converter, but i would rather not read it until i am finished (also i'm pretty sure it will be way cleaner/efficient than the one i am working on).
    There's nothing wrong with implementing your own converter for a learning exercise but it's a good idea to use the parse method to verify your results.
    Last edited by ChristopherLowe; July 17th, 2014 at 02:00 AM.

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

    stylelink (July 17th, 2014)

Similar Threads

  1. Convert hexadecimal to decimal WITHOUT using integer.parseInt method
    By carlos_canas94 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 12th, 2014, 07:01 PM
  2. Replies: 4
    Last Post: February 25th, 2014, 11:00 AM
  3. Converting from decimal to hexadecimal
    By BTroj in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 24th, 2014, 09:55 PM
  4. convertion hexadecimal to decimal and binary to decimal
    By Md.Ashraful Haque in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 13th, 2013, 07:30 AM
  5. Decimal to Hexadecimal, Without Java Libraries.
    By ribmant in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 3rd, 2012, 07:28 AM

Tags for this Thread