English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Program to Convert Decimal to Hexadecimal

There are two methods to convert decimal to hexadecimal.

method1-usetoString()method, as the base of hexadecimal is16therefore, it can be set through the base to16easily convert decimal to hexadecimal.

Example

public class Demo {
   public static void main(String args[]) {
      int dec = 30;
      //Convert to hexadecimal
      String hex = Integer.toString(dec, 16);
      System.out.println(hex);
   }
}

Output Result

1e

method2-usetoHexString()method to convert decimal to hexadecimal;

Example

public class Demo {
   public static void main(String args[]) {
      int dec = 30;
      //Convert to hexadecimal
      String hex = Integer.toHexString(dec);
      System.out.println(hex);
   }
}

Output Result

1e