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

How to Convert Decimal to Hexadecimal in Java

To convert decimal to hexadecimal, use one of the following two methods:

  • Integer.toHexString() -It returns the string representation of the integer parameter, with a base of16unsigned integer with a base of

  • Integer.parseInt() -It also allows you to set the base, for example, for hexadecimal, set it to16.

Now let's look at an example of using the Integer.toHexString() method to convert decimal to hexadecimal.

Example

public class Demo {
   public static void main(String args[]) {
      int dec = 158;
      System.out.println(Integer.toHexString(dec));
   }
}

Output Result

9e

Now let's look at an example of using the Integer.parseInt() method to convert decimal to hexadecimal.

Example

public class Demo {
   public static void main(String args[]) {
      String str = "3d8";
      System.out.println(Integer.parseInt(str, 16));
   }
}

Output Result

984