Package de.uplanet.util.encoder
Class HexEncoder
java.lang.Object
de.uplanet.util.encoder.HexEncoder
Utility class for hex encoding and decoding.
-
Method Summary
Modifier and TypeMethodDescriptionstatic char[]byteToHex(byte p_byte) Returns a character array of 2 hexadecimal digits corresponding to abyte.static char[]charToHex(int p_iChar) Returns a character array of 4 hexadecimal digits (most significant digit first) corresponding to achar.static byte[]decode(char[] p_achSrc) Returns a byte array from a character array of hexadecimal digits.static byte[]decode(char[] p_achSrc, int p_iStart, int p_iLen) Returns a byte array from a character array of hexadecimal digits.static byte[]decode(CharSequence p_chseq, int p_iStart, int p_iLen) Returns a byte array from a character sequence of hexadecimal digits.static char[]encode(byte[] p_buf) Returns a character array of hexadecimal digits from a byte array.static char[]encode(byte[] p_buf, int p_iOffset, int p_iLength) Returns a string of hexadecimal digits from a byte array, starting at p_iOffset with length p_iLenghth.static inthexDigitToInt(char p_ch) Returns the value of a hexadecimal digit as anint.static char[]intToHex(int p_iArg) Returns a character array of 8 hexadecimal digits (most significant digit first) corresponding to anint.
-
Method Details
-
hexDigitToInt
Returns the value of a hexadecimal digit as anint.- Parameters:
p_ch- Hexadecimal digit.- Returns:
- Integer that corresponds to the hexadecimal digit.
- Throws:
IllegalArgumentException- If the digit is not hexadecimal.
-
byteToHex
public static char[] byteToHex(byte p_byte) Returns a character array of 2 hexadecimal digits corresponding to abyte.- Parameters:
p_byte- The byte to encode.- Returns:
- Hexadecimal representation of the byte.
-
charToHex
public static char[] charToHex(int p_iChar) Returns a character array of 4 hexadecimal digits (most significant digit first) corresponding to achar.Note: Not all codepoints could be represented by two-byte sequences. So use this method only when you know what you're doing.
- Parameters:
p_iChar- The character to encode.- Returns:
- A hexadecimal two-byte representation of the character.
-
intToHex
public static char[] intToHex(int p_iArg) Returns a character array of 8 hexadecimal digits (most significant digit first) corresponding to anint.- Parameters:
p_iArg- The integer to encode.- Returns:
- A hexadecimal four byte representation of the integer.
-
encode
public static char[] encode(byte[] p_buf) Returns a character array of hexadecimal digits from a byte array.- Parameters:
p_buf- The byte array to encode.- Returns:
- A character array of hexadecimal digits that represents the byte array.
-
encode
public static char[] encode(byte[] p_buf, int p_iOffset, int p_iLength) Returns a string of hexadecimal digits from a byte array, starting at p_iOffset with length p_iLenghth.- Parameters:
p_buf- The byte array to encode.p_iOffset- Offset to start encoding.p_iLength- Number of Bytes to encode.- Returns:
- A character array of hexadecimal digits that represents the memory block.
-
decode
public static byte[] decode(char[] p_achSrc) throws de.uplanet.util.encoder.EncoderException Returns a byte array from a character array of hexadecimal digits.- Parameters:
p_achSrc- Character array of hexadecimal digits.- Returns:
- The decoded byte array or
nullif the input isnullor has zero length. - Throws:
de.uplanet.util.encoder.EncoderException- If the input length is odd or if the input contains non-hexadecimal characters.
-
decode
public static byte[] decode(char[] p_achSrc, int p_iStart, int p_iLen) throws de.uplanet.util.encoder.EncoderException Returns a byte array from a character array of hexadecimal digits.- Parameters:
p_achSrc- Character array of hexadecimal digits.p_iStart- The start index for decoding.p_iLen- The number of hexadecimal digits to process.- Returns:
- The decoded byte array.
- Throws:
IllegalArgumentException- Ifp_achSrcisnull.de.uplanet.util.encoder.EncoderException- Ifp_iLenis odd or if the input contains non-hexadecimal characters.
-
decode
public static byte[] decode(CharSequence p_chseq, int p_iStart, int p_iLen) throws de.uplanet.util.encoder.EncoderException Returns a byte array from a character sequence of hexadecimal digits.- Parameters:
p_chseq- A character sequence of hexadecimal digits.p_iStart- The start index for decoding.p_iLen- The number of hexadecimal digits to process.- Returns:
- The decoded byte array or
nullif p_achSrc isnullor p_iLen is0. - Throws:
IllegalArgumentException- Ifp_chseqisnull.de.uplanet.util.encoder.EncoderException- Ifp_iLenis odd or if the input contains non-hexadecimal characters.
-