Class Hex

java.lang.Object
org.apache.shiro.lang.codec.Hex

public final class Hex extends Object
Hexadecimal encoder and decoder.

This class was borrowed from Apache Commons Codec SVN repository (rev. 560660) with modifications to enable Hex conversion without a full dependency on Commons Codec. We didn't want to reinvent the wheel of great work they've done, but also didn't want to force every Shiro user to depend on the commons-codec.jar

As per the Apache 2.0 license, the original copyright notice and all author and copyright information have remained in tact.

Since:
0.9
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    decode(byte[] array)
    Converts an array of character bytes representing hexadecimal values into an array of bytes of those same values.
    static byte[]
    decode(char[] data)
    Converts an array of characters representing hexadecimal values into an array of bytes of those same values.
    static byte[]
    Converts the specified Hex-encoded String into a raw byte array.
    static char[]
    encode(byte[] data)
    Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
    static String
    encodeToString(byte[] bytes)
    Encodes the specified byte array to a character array and then returns that character array as a String.
    protected static int
    toDigit(char ch, int index)
    Converts a hexadecimal character to an integer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • encodeToString

      public static String encodeToString(byte[] bytes)
      Encodes the specified byte array to a character array and then returns that character array as a String.
      Parameters:
      bytes - the byte array to Hex-encode.
      Returns:
      A String representation of the resultant hex-encoded char array.
    • encode

      public static char[] encode(byte[] data)
      Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. The returned array will be double the length of the passed array, as it takes two characters to represent any given byte.
      Parameters:
      data - byte[] to convert to Hex characters
      Returns:
      A char[] containing hexadecimal characters
    • decode

      public static byte[] decode(byte[] array) throws IllegalArgumentException
      Converts an array of character bytes representing hexadecimal values into an array of bytes of those same values. The returned array will be half the length of the passed array, as it takes two characters to represent any given byte. An exception is thrown if the passed char array has an odd number of elements.
      Parameters:
      array - An array of character bytes containing hexadecimal digits
      Returns:
      A byte array containing binary data decoded from the supplied byte array (representing characters).
      Throws:
      IllegalArgumentException - Thrown if an odd number of characters is supplied to this function
      See Also:
    • decode

      public static byte[] decode(String hex)
      Converts the specified Hex-encoded String into a raw byte array. This is a convenience method that merely delegates to decode(char[]) using the argument's hex.toCharArray() value.
      Parameters:
      hex - a Hex-encoded String.
      Returns:
      A byte array containing binary data decoded from the supplied String's char array.
    • decode

      public static byte[] decode(char[] data) throws IllegalArgumentException
      Converts an array of characters representing hexadecimal values into an array of bytes of those same values. The returned array will be half the length of the passed array, as it takes two characters to represent any given byte. An exception is thrown if the passed char array has an odd number of elements.
      Parameters:
      data - An array of characters containing hexadecimal digits
      Returns:
      A byte array containing binary data decoded from the supplied char array.
      Throws:
      IllegalArgumentException - if an odd number or illegal of characters is supplied
    • toDigit

      protected static int toDigit(char ch, int index) throws IllegalArgumentException
      Converts a hexadecimal character to an integer.
      Parameters:
      ch - A character to convert to an integer digit
      index - The index of the character in the source
      Returns:
      An integer
      Throws:
      IllegalArgumentException - if ch is an illegal hex character