How to convert a byte array to a Hex String in Java

When processing binary values it is very difficult to read or to display them because any printing function generates a String value.  The problem with this approach is that not all byte values can be interpreted as a printable char (i.e. the 0 binary value represents the NUL symbol; for more details check ASCII Codes + HTML Codes and Special Characters) and the resulting String will not contain all the byte values or it will not be accurate. Moreover in Java a char is stored on 2 bytes.

In this post we will see how to convert a byte array to a Hex String in a Java application. The solution is useful because:

  • printing binary values in base 2 or base 10 format can become difficult to read as the value can have multiple digits;
  • is easier to read values in hexadecimal base;
  • it is easier to check or compare values in hexadecimal base.

In Cryptography, Hex Strings are always used to display hash values or parts of a cipher because most of the time the resulting values are at byte level and don’t have a meaning as regular Strings.

Why not to directly convert byte arrays to string values

A reason for not using a direct conversion from byte to String is the ASCII table (ASCII Codes + HTML Codes and Special Characters) that proves that not all byte values are printable. For example, the next sequence

	byte[] byteArray = {'t','e',103,0,66,8,6,'s','t'};
        String value1 = new String(byteArray);
        System.out.println(value1);

        byte[] anotherByteArray = {'t','e',103,0,66,-8,6,'s','t'};
        String value2 = new String(anotherByteArray);
        System.out.println(value2);

prints something that at console looks like this

teg B  st		//9 chars
teg B�		//only 6 chars

because 103 is ASCII code for g, 0 is ASCII code for null, 66 for B, 8 is the ASCII code for backspace and 6 for acknowledge. The –8 value is interpreted as 248 (11111000 in binary), meaning latin small letter o with slash.

Despite the difference between the displayed chars and the real ASCII values in the Srings, there is a BIG difference between the number of chars of the two strings. The two byte arrays have the same number of elements, but the their String versions have 9, respectively 6.

So, DO NOT store or print byte arrays as regular Strings, but as Hex Strings, or maybe Base64.

Convert a byte array into a Hex string

In order to convert a byte array to a Hex String are used:

  • an array of 16 hexadecimal symbols (from 0 to 9,A,B,C,D,E,F);
  • logical bitwise AND at bit level (remember only 1 AND 1 = 1);
  • bit masks as 0xFF (111111112) or 0X0F (000011112);
  • logical shift operation (>>);

The method used to convert the byte array is:

        public static char[] getHexValue(byte[] array) {
                char[] symbols = "0123456789ABCDEF".toCharArray();
                char[] hexValue = new char[array.length * 2];

                for (int i = 0; i < array.length; i++) {

                        // convert the byte to an int
                        int current = array[i] & 0xff;

                        // determine the Hex symbol for the last 4 bits
                        hexValue[i * 2 + 1] = symbols[current & 0x0f];
                        
                        // determine the Hex symbol for the first 4 bits
                        hexValue[i * 2] = symbols[current >> 4];
                }
                return hexValue;
        }

Using the method to display the previous two examples of byte arrays, you get:

746567004208067374
7465670042F8067374

The method can also be used to verify results in Java applications that implement cryptographic algorithms, like:

Like it? Then share this post or check the external adds. Sharing is the best way to appreciate the author.