Integer.toHexString() method in Java

toHexString() method is a static method of the Integer wrapper class that is used to return the hex string of the integer passed in the argument as an unsigned integer. In this post, we are going to look at the toHexString() method in detail.

  • Method declaration – public static String toHexString(int i)
  • What does it do? It will take an integer as an argument and then will convert it into the hex string by processing the input as an unsigned integer
  • What does it return? It will return the hex string

A hex string can contain the following characters –

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E

Now, what are signed and unsigned integers? Signed integers contain both positive and negative numbers and their range is from [-2147483648 to 2147483647] whereas unsigned integers contain only the non-negative numbers ranging from [0 to 4294967295].

In this post, we will see how we can convert both positive and negative into their corresponding hex strings.

Using toHexString() method on a positive integer

Positive integers can easily be represented by the toHexString() method. Let’s see the hex string of 100.

public class Codekru {

	public static void main(String[] args) {

		int i = 100;

		String hexString = Integer.toHexString(i);

		System.out.println("Hex String representation of " + i + " is: " + hexString);

	}

}

Output –

Hex String representation of 100 is: 64
Using toHexString() method on a negative integer

Negative numbers cannot be directly converted to their corresponding hex strings. So, what toHexString() does is, it first adds 232(4294967296) to the negative number and then returns the hex string of the resulting number.

So, let’s try to find the hex string representation of the number -100. Now, toHexString() will do the following –

  • Add 232(4294967296) with the number ( -100 + 232 )
  • Returns the hex string representation of the resulting number ( 4294967196 )
public class Codekru {

	public static void main(String[] args) {

		int i = -100;

		String hexString = Integer.toHexString(i);

		System.out.println("Hex String representation of " + i + " is: " + hexString);

	}

}

Output –

Hex String representation of -100 is: ffffff9c

So, we can say that the hex string representation of -100 and 4294967196 is the same and that is “ffffff9c”.

How do we get back the original number from the hex string?

We can easily get the original number back by using the parseUnsignedInt() method where the base would be 16. So, let’s try to convert the hex strings that we got in the last two examples.

public class Codekru {

	public static void main(String[] args) {

		String hexStringForPositiveNumber = "64";
		String hexStringForNegativeNumber = "ffffff9c";

		int positiveNumber = Integer.parseUnsignedInt(hexStringForPositiveNumber, 16);
		int negativeNumber = Integer.parseUnsignedInt(hexStringForNegativeNumber, 16);

		System.out.println("Positive Number: " + positiveNumber);
		System.out.println("Negative Number: " + negativeNumber);

	}
}

Output –

Positive Number: 100
Negative Number: -100

Please visit this link if you want to know more about the Integer wrapper class of java and its other functions or methods.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Related Articles
Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *