Javatpoint Logo
Javatpoint Logo

Java how to Convert Bytes to Hex?

In this section, we will learn different approaches to convert bytes to hexadecimal in Java.

Convert Bytes to Hex

There are the following ways to convert bytes to hexadecimal:

  • Using Integer.toHexString() Method
  • Using String.format() Method
  • Using Byte Operation

Using Integer.toHexString() Method

It is a built-in function of the java.lang.Integer class.


The function accepts a single parameter of type int num that is to be converted into hexadecimal. It returns the converted hexadecimal string (base 16) of the given number.

Note: The function does not accept numbers as strings and double or floating-point numbers.

For example, if we provide 11 as input, it returns b which is equivalent hexadecimal of 11.

Java Programs to Convert Byte to Hexadecimal


Byte = -52
Hexadecimal Equivalent= cc

Let's see another approach.


Hexadecimal string is: d4
Hexadecimal string is: 22
Hexadecimal string is: ffffffec

Convert Byte Array to Hexadecimal

We can also convert bytes array to hexadecimal. The following Java program demonstrates the same.


00 09 03 FF 05 08 FE

Using String.format() Method

In the following Java program, we have defined an array of type byte. After that, defined a loop that iterates over the byte array. In order to convert the byte array to a hex value, we have used the function String.format(). Inside the function, we have passed %02X that prints two places of hexadecimal (X) value and the same store in the variable str.


The method accepts two parameters:

format: It is the format of the string.

args: Arguments for the format string. It may be zero or more.

It returns the formatted string. It throws NullPointerException if the format is null and throws IllegalFormatException if the format is illegal or incompatible.

It is a very slow process to convert the byte array to hexadecimal.


0B	05	13	38	

Using Byte Operation


17	15	12	0B

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA