Convert Integer to Hex String in Java



The Integer.toHexString() method in Java converts Integer to hex string.

Let’s say the following are our integer values.

int val1 = 5;
int val2 = 7;
int val3 = 13;

Convert the above int values to hex string.

Integer.toHexString(val1);
Integer.toHexString(val2);
Integer.toHexString(val3);

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      int val1 = 5;
      int val2 = 7;
      int val3 = 13;
      int val4 = 23;
      int val5 = 30;
      System.out.println("Converting integer "+val1+" to Hex String: "+Integer.toHexString(val1));
      System.out.println("Converting integer "+val2+" to Hex String: "+Integer.toHexString(val2));
      System.out.println("Converting integer "+val3+" to Hex String: "+Integer.toHexString(val3));
      System.out.println("Converting integer "+val4+" to Hex String: "+Integer.toHexString(val4));
      System.out.println("Converting integer "+val5+" to Hex String: "+Integer.toHexString(val5));
   }
}

Output

Converting integer 5 to Hex String: 5
Converting integer 7 to Hex String: 7
Converting integer 13 to Hex String: d
Converting integer 23 to Hex String: 17
Converting integer 30 to Hex String: 1e
karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know


Advertisements