How to convert Decimal to Hexadecimal in Java


To convert decimal to hexadecimal, use any of the two methods i.e.

  • Integer.toHexString() − It returns a string representation of the integer argument as an unsigned integer in base 16.

  • Integer.parseInt() − It allows you to set the radix as well, for example, for hexadecimal set it as 16.

Let us see an example now to convert decimal to hexadecimal using Integer.toHexString() method.

Example

 Live Demo

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

Output

9e

Let us see an example now to convert decimal to hexadecimal using Integer.parseInt() method.

Example

 Live Demo

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

Output

984

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements