Class HexEncoder

java.lang.Object
de.uplanet.util.encoder.HexEncoder

@Scriptable public final class HexEncoder extends Object
Utility class for hex encoding and decoding.
  • Method Summary

    Modifier and Type
    Method
    Description
    static char[]
    byteToHex(byte p_byte)
    Returns a character array of 2 hexadecimal digits corresponding to a byte.
    static char[]
    charToHex(int p_iChar)
    Returns a character array of 4 hexadecimal digits (most significant digit first) corresponding to a char.
    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 int
    hexDigitToInt(char p_ch)
    Returns the value of a hexadecimal digit as an int.
    static char[]
    intToHex(int p_iArg)
    Returns a character array of 8 hexadecimal digits (most significant digit first) corresponding to an int.

    Methods inherited from class java.lang.Object

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

    • hexDigitToInt

      public static int hexDigitToInt(char p_ch) throws IllegalArgumentException
      Returns the value of a hexadecimal digit as an int.
      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 a byte.
      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 a char.

      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 an int.
      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 null if the input is null or 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 - If p_achSrc is null.
      de.uplanet.util.encoder.EncoderException - If p_iLen is 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 null if p_achSrc is null or p_iLen is 0.
      Throws:
      IllegalArgumentException - If p_chseq is null.
      de.uplanet.util.encoder.EncoderException - If p_iLen is odd or if the input contains non-hexadecimal characters.