Convert Integer to Hex String in Java



In Java, converting an integer value to a hexadecimal (hex) string means transforming the number from its base-10 (decimal) format to base-16 format. This conversion uses digits 0-9 and letters A to F to represent the values.

  • Integer: An integer is a whole number without having a fractional part, such as -2,-1,0,1,2, etc.
  • Hex String: A hexadecimal (hex) string represents a number in base-16, using digits 0-9 and letters A-F. We represent the A-F in numbers as 10 to 15.

Example

Let's say the following are our integer values.

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

Converting the above int values to hex string.

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

After converting the int values to hex string it results as:

HexString(val1) = 5 HexString(val2) = 7 HexString(val3) = d

In this entire article, we will use the single method named toHexString() to convert the given integer values to a hexadecimal String.

The toHexString() Method

In Java, the toHexString() is a static method belonging to the Integer Class. It accepts a number as a parameter and converts that number to a hexadecimal string.

Since it is a static method, we can directly invoke it by using the Integer class as Integer.toHexString().

Example 1: Basic Example

The following program converts a given integer value 94 to its hexadecimal string using an Integer.toHexString() method ?

Open Compiler
public class Main { public static void main(String[] args) { int number = 94; String hexString = Integer.toHexString(number); System.out.println("Hexadecimal of " + number + " = " + hexString); } }

Output

The above displays the hex string of a number "94" as ?

Hexadecimal of 94 = 5e

Example 2: Hexadecimal String of a Negative Integer

In this program, we use the Integer.toHexString() method to convert a given negative integer value -255 to hexadecimal string ?

Open Compiler
public class Main { public static void main(String[] args) { int number = -255; String hexString = Integer.toHexString(number); System.out.println("Hexadecimal of " + number + " = " + hexString); } }

Output

Following is the output of the above program ?

Hexadecimal of -255 = ffffff01

Example 3: Converting Multiple Integer Values to hex String

In the example below, we use the for-loop to iterate through the number in the range 8 to 12 and use the Integer.toHexString() method inside the loop to convert each integer value to its hexadecimal string ?

Open Compiler
public class Main { public static void main(String[] args) { for (int i = 8; i <= 12; i++) { String hexString = Integer.toHexString(i); System.out.println("Hexadecimal of " + i + " = " + hexString); } } }

Output

The above programs display hexadecimal strings of numbers 8 to 12 ?

Hexadecimal of 8 = 8
Hexadecimal of 9 = 9
Hexadecimal of 10 = a
Hexadecimal of 11 = b
Hexadecimal of 12 = c
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2024-12-14T17:03:48+05:30

364 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements