Tuesday, April 24, 2012

Tutorial: Decimal to Hexadecimal Converter in Java

Hi guys! I'm here again. Today, I will show you a program I made in Java that can convert an input Decimal number to its equivalent Hexadecimal value. It is very easy to do this because. Just follow the rules in converting from decimal to hexadecimal number in number system. Take note that there are 16 values or digits used in hexadecimal number system which is the number from 0 to 9, and the first six (6) letters in the alphabet which is from A to F that corresponds to the number 10 to 15, respectively.

See the images below. Download the sample program source code and try it on your own.







The image above indicates the user enter a decimal value which is 712 then program converts it to its hexadecimal value which us 2C8.

If you want to make sure that this program works properly and correctly, you can try the built calculator of your PC or laptop (see the image below).





I tested the sample input number using the built-in calculator in my laptop. So, as you can see, it works!




The figure above shows that when the user input a non-integer value then an error message will appear. I used the concept of the try and catch method to avoid termination of the program when an error encountered during run-time.

The Program Source Code:

/* Author: Vilchor G. Perdido
 * Project: DecimalToHexadecimalConverter v.1.0
 * Date: February 14, 2012
 * Website: http://www.eturo.co.cc
 * Email: vgperdido221020@yahoo.com.ph
 */

import java.util.*;

public class DecimalToHexaConverter
{
    public static void main(String[] args)
    {
        Scanner read = new Scanner(System.in);
        int quotient = 0;
        final int BASE = 16;
        String choice = "";
        char sagot = ' ';

        System.out.println("======================================");
        System.out.println("DECIMAL TO HEXADECIMAL CONVERTER PROGRAM");
        System.out.println("=====================================");

        do {
            try {
                System.out.println("Please input DECIMAL number to convert:");
                int whole = read.nextInt();
                int x=0;
                int temp=0;
                int dividend=0;
                char octal[] = new char[10];
                dividend = whole;
                System.out.println("\nThe HEXADECIMAL value is:");

                do
                {
                    quotient = dividend/BASE;
                    int remainder = (dividend - (quotient*BASE));
                    dividend = quotient;

                    if(remainder==10)
                    {
                        char z = (int)65;
                        System.out.print(z);
                        //octal[x] = z;
                    }
                    else if(remainder==11)
                    {
                        char z = (int)66;
                        //System.out.print(z);
                        octal[x] = z;
                    }
                    else if(remainder==12)
                    {
                        char z = (int)67;
                        //System.out.print(z);
                        octal[x] = z;
                    }
                    else if(remainder==13)
                    {
                        char z = (char)68;
                        //System.out.print(z);
                        octal[x] = z;
                    }
                    else if(remainder==14)
                    {
                        char z = (int)69;
                        //System.out.print(z);
                        octal[x] = z;
                    }
                    else if(remainder==15)
                    {
                        char z = (int)70;
                        //System.out.print(z);
                        octal[x] = z;
                    }
                    else
                    {
                        //System.out.print(remainder);
                        if(remainder == 0 )
                        {
                            char z = (int)48;
                            octal[x]=z;
                            //char z = (char)remainder;
                            //System.out.print(remainder);
                        }
                        else if(remainder == 1 )
                        {
                            char z = (int)49;
                            octal[x]=z;
                            //char z = (char)remainder;
                            //System.out.print(remainder);
                        }
                        else if(remainder == 2 )
                        {
                            char z = (int)50;
                            octal[x]=z;
                        }
                        else if(remainder == 3 )
                        {
                            char z = (int)51;
                            octal[x]=z;
                        }
                        else if(remainder == 4 )
                        {
                            char z = (int)52;
                            octal[x]=z;
                        }
                        else if(remainder == 5 )
                        {
                            char z = (int)53;
                            octal[x]=z;
                        }
                        else if(remainder == 6 )
                        {
                            char z = (int)54;
                            octal[x]=z;
                        }
                        else if(remainder == 7 )
                        {
                            char z = (int)55;
                            octal[x]=z;
                        }
                        else if(remainder == 8 )
                        {
                            char z = (int)56;
                            octal[x]=z;
                        }
                        else if(remainder == 9 )
                        {
                            char z = (int)57;
                            octal[x]=z;
                        }
                    }

                    ++x;

                }while(dividend!=0);

                for (temp = x-1;temp >= 0;temp--)
                {
                    System.out.print(octal[temp]);
                }

            }
            catch(InputMismatchException e)
            {
                read = new Scanner(System.in);
                System.out.println("=========WARNING MESSAGE!!!==========");
                System.out.println("\tWrong or Incorrect Input");
                System.out.println("=====================================");

            }
            System.out.println("\n\nWhat do you want? Convert again?");
            System.out.println("Press Y or y to continue or any key to quit.");
            System.out.print("Confirm choice:");
            choice = read.next();
            sagot = choice.charAt(0);
            System.out.println("");

        }while(sagot=='Y' || sagot=='y');
        System.out.println("Program terminated.....");
    }
}



Happy coding guys! You can learn more here, just visit it regularly.

Thank you guys! Hope you like it. Please leave some comments...

Like Us on Facebook

Related Posts Plugin for WordPress, Blogger...