Class Hex
java.lang.Object
org.apache.shiro.lang.codec.Hex
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 TypeMethodDescriptionstatic 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 StringencodeToString(byte[] bytes) Encodes the specified byte array to a character array and then returns that character array as a String.protected static inttoDigit(char ch, int index) Converts a hexadecimal character to an integer.
-
Method Details
-
encodeToString
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
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
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
Converts the specified Hex-encoded String into a raw byte array. This is a convenience method that merely delegates todecode(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
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
Converts a hexadecimal character to an integer.- Parameters:
ch- A character to convert to an integer digitindex- The index of the character in the source- Returns:
- An integer
- Throws:
IllegalArgumentException- if ch is an illegal hex character
-