Java Program to convert from decimal to hexadecimal



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
karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know


Advertisements