Class Hex


  • public class Hex
    extends java.lang.Object
    Encode and decode to/from hexadecimal strings to byte arrays.
    • Constructor Summary

      Constructors 
      Constructor Description
      Hex()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] decode​(java.lang.String hexStr)
      Decode hexadecimal-encoded string and return raw byte array.
      static java.lang.String encode​(byte[] b, boolean leading0x)
      Output byte representation as hexadecimal representation.
      static byte[] fromHex​(java.lang.String hexStr)
      Decode hexadecimal-encoded string and return raw byte array.
      static java.lang.String toHex​(byte[] b, boolean leading0x)
      Output byte representation as hexadecimal representation.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Hex

        public Hex()
    • Method Detail

      • toHex

        public static java.lang.String toHex​(byte[] b,
                                             boolean leading0x)
        Output byte representation as hexadecimal representation.
        Parameters:
        b - Bytes to encode to hexadecimal representation.
        leading0x - If true, return with leading "0x".
        Returns:
        Hexadecimal representation of specified bytes.
      • encode

        public static java.lang.String encode​(byte[] b,
                                              boolean leading0x)
        Output byte representation as hexadecimal representation. Alias for toHex() method.
        Parameters:
        b - Bytes to encode to hexadecimal representation.
        leading0x - If true, return with leading "0x".
        Returns:
        Hexadecimal representation of specified bytes.
      • fromHex

        public static byte[] fromHex​(java.lang.String hexStr)
        Decode hexadecimal-encoded string and return raw byte array. Important note: This method preserves leading 0 filled bytes on the conversion process, which is important for cryptographic operations in dealing with things like keys, initialization vectors, etc. For example, the string "0x0000face" is going to return a byte array whose length is 4, not 2.
        Parameters:
        hexStr - Hexadecimal-encoded string, with or without leading "0x".
        Returns:
        The equivalent byte array.
      • decode

        public static byte[] decode​(java.lang.String hexStr)
        Decode hexadecimal-encoded string and return raw byte array. Alias for fromHex() method.
        Parameters:
        hexStr - Hexadecimal-encoded string, with or without leading "0x".
        Returns:
        The equivalent byte array.