Java Program to convert from decimal to hexadecimal

Java 8Object Oriented ProgrammingProgramming

To convert decimal to hexadecimal, here are the two methods.

Method 1 − Using toString() method you can easily convert decimal to hexadecimal by setting the radix as 16, since hexadecimal radix is 16.

Example

 Live Demo

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

Output

1e

Method2 − Use toHexString() method to convert decimal to hexadecimal;

Example

 Live Demo

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

Output

1e
raja
Published on 13-Nov-2018 11:52:42
Advertisements