Home Java Tutorials Decimal to Hexadecimal Java Source Code Using toHexString Method

Software Configurations

Follow Me

Decimal to Hexadecimal Java Source Code Using toHexString Method

 

This java source code will show how to convert decimal number to hexadecimal number. To convert decimal to hexadecimal used toHexString (Integer) method.

 

DecimaltoHexadecimal.java

 

//copyrighted by topsourcecode.com
import javax.swing.JOptionPane;

public class DecimaltoHexadecimal
{
 public static void main(String []a)
 {
    //Method 1
    String sNumber=JOptionPane.showInputDialog("Insert a Integer value");
    int number=0;
    try
    {
      number=Integer.parseInt(sNumber);
    }
    catch(Exception e)
    {
       JOptionPane one = new JOptionPane();
       JOptionPane.showMessageDialog(one,"Input value error(Expected a decimal value)");
       System.exit(-1);
    }
    
    String hexadecimal = Integer.toHexString(number);
    JOptionPane one = new JOptionPane();
    JOptionPane.showMessageDialog(one,"Hexadecimal Value of Decimal "+number+" =\n" + hexadecimal);
 }
}

 

Result


Share this post