Convert Byte Array to Hexadecimal in Java


Write a Java Program to convert byte array to hexadecimal.

Click Here to View the Solution!
public class ByteHex {
     public static void main(String[] args) {
         byte[] bytes = {13, 27, 45, 112};     
         for (byte b : bytes) {
             String st = String.format("%02X", b);
Click Here to View the Output!
Click Here to View the Explanation!
  • In this program, an array named ‘bytes’ is created which contains some elements in it. This array is of byte type.
  • The string method format() is used for converting every element in the byte array to its corresponding hexadecimal value through continuous iterations.
  • The .format() method formats the input byte to %02X which means 02 places of hex (X) digits are displayed. This converted value is stored in the variable st.

%d bloggers like this: