Class HexConverter

java.lang.Object
eu.svjatoslav.commons.data.HexConverter

public class HexConverter extends Object
Converts byte arrays to hexadecimal string representation.

This utility class provides a simple method to convert binary data into a human-readable hexadecimal format. Each byte is converted to two uppercase hexadecimal characters (0-9, A-F).

Example usage:


 byte[] data = new byte[]{0x01, 0x02, 0xFF};
 String hex = HexConverter.byteArrayToHex(data);
 // Result: "0102FF"

 byte[] empty = new byte[]{};
 String emptyHex = HexConverter.byteArrayToHex(empty);
 // Result: ""

 String nullHex = HexConverter.byteArrayToHex(null);
 // Result: null
 

This class is useful for:

  • Displaying binary data in logs or debug output
  • Creating checksums or hash representations
  • Encoding binary data for text-based protocols
  • Comparing byte arrays visually
  • Method Details

    • byteArrayToHex

      public static String byteArrayToHex(byte[] bytes)
      Converts a byte array to an uppercase hexadecimal string.

      Each byte is converted to exactly two hexadecimal characters. The high nibble (bits 7-4) becomes the first character, and the low nibble (bits 3-0) becomes the second character.

      Example conversions:

      • byte 0x00 → "00"
      • byte 0x0F → "0F"
      • byte 0xFF → "FF"
      • byte 0xAB → "AB"

      The result is always uppercase. Empty arrays return an empty string. Null arrays return null.

      Parameters:
      bytes - the byte array to convert. May be null or empty.
      Returns:
      an uppercase hexadecimal string representation, or null if the input was null. The string length is always twice the byte array length.