How to convert Decimal to Hexadecimal in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 13-Nov-2018 07:24:22
Advertisements