Class StringHex

java.lang.Object
com.helger.base.string.StringHex

@Immutable public class StringHex extends Object
Helper class for hexadecimal string encoding and decoding of byte arrays and individual values.
Author:
Philip Helger
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    getHexByte(char cHigh, char cLow)
     
    static char
    getHexChar(int n)
    Get the matching hex digit as a lower case character.
    static char
    Get the matching hex digit as an upper case character.
    static byte @NonNull []
    getHexDecoded(byte @NonNull [] aInput)
    Decode a hex encoded byte array to a byte array.
    static byte @NonNull []
    getHexDecoded(byte @NonNull [] aInput, int nOfs, int nLen)
    Decode a hex encoded byte array to a byte array.
    static byte @NonNull []
    getHexDecoded(char @NonNull [] aInput)
    Decode a hex encoded char array to a byte array.
    static byte @NonNull []
    getHexDecoded(char @NonNull [] aInput, int nOfs, int nLen)
    Decode a hex encoded char array to a byte array.
    static byte @NonNull []
    getHexDecoded(@NonNull String sInput)
    Decode a hex encoded string to a byte array.
    static @NonNull String
    getHexEncoded(byte @NonNull [] aInput)
    Convert a byte array to a hexadecimal encoded string.
    static @NonNull String
    getHexEncoded(byte @NonNull [] aInput, int nOfs, int nLen)
    Convert a byte array to a hexadecimal encoded string.
    static @NonNull String
    getHexEncoded(@NonNull String sInput, @NonNull Charset aCharset)
    Convert a string to a byte array and than to a hexadecimal encoded string.
    static byte @NonNull []
    getHexEncodedByteArray(byte @NonNull [] aInput)
    Convert a byte array to a hexadecimal encoded byte array.
    static byte @NonNull []
    getHexEncodedByteArray(byte @NonNull [] aInput, int nOfs, int nLen)
    Convert a byte array to a hexadecimal encoded byte array.
    static @NonNull String
    getHexString(byte nValue)
    Convert a byte value to a hex string.
    static @NonNull String
    getHexString(int nValue)
    Convert an int value to a hex string.
    static @NonNull String
    getHexString(long nValue)
    Convert a long value to a hex string.
    static @NonNull String
    getHexString(short nValue)
    Convert a short value to a hex string.
    static @NonNull String
    getHexStringLeadingZero(byte nValue, int nDigits)
    Convert a byte value to a hex string with leading zeros up to the specified number of digits.
    static @NonNull String
    getHexStringLeadingZero(int nValue, int nDigits)
    Convert an int value to a hex string with leading zeros up to the specified number of digits.
    static @NonNull String
    getHexStringLeadingZero(long nValue, int nDigits)
    Convert a long value to a hex string with leading zeros up to the specified number of digits.
    static @NonNull String
    getHexStringLeadingZero(short nValue, int nDigits)
    Convert a short value to a hex string with leading zeros up to the specified number of digits.
    static @NonNull String
    Convert a byte value to a hex string with a leading zero to ensure at least 2 digits.
    static int
    getHexValue(char c)
    Get the decimal value of the passed hex character

    Methods inherited from class java.lang.Object

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

    • getHexChar

      public static char getHexChar(int n)
      Get the matching hex digit as a lower case character.
      Parameters:
      n - The value to get the hex digit from. Must be between 0 and 15.
      Returns:
      The hex character (one of 0-9 or a-f), or '\0' if the value could not be converted
    • getHexCharUpperCase

      public static char getHexCharUpperCase(int n)
      Get the matching hex digit as an upper case character.
      Parameters:
      n - The value to get the hex digit from. Must be between 0 and 15.
      Returns:
      The hex character (one of 0-9 or A-F), or '\0' if the value could not be converted
    • getHexEncoded

      public static @NonNull String getHexEncoded(@NonNull String sInput, @NonNull Charset aCharset)
      Convert a string to a byte array and than to a hexadecimal encoded string.
      Parameters:
      sInput - The source string. May not be null.
      aCharset - The charset to use. May not be null.
      Returns:
      The String representation of the byte array of the string.
    • getHexEncoded

      public static @NonNull String getHexEncoded(byte @NonNull [] aInput)
      Convert a byte array to a hexadecimal encoded string.
      Parameters:
      aInput - The byte array to be converted to a String. May not be null.
      Returns:
      The String representation of the byte array.
    • getHexEncoded

      public static @NonNull String getHexEncoded(byte @NonNull [] aInput, int nOfs, int nLen)
      Convert a byte array to a hexadecimal encoded string.
      Parameters:
      aInput - The byte array to be converted to a String. May not be null.
      nOfs - Byte array offset
      nLen - Number of bytes to encode
      Returns:
      The String representation of the byte array.
    • getHexEncodedByteArray

      public static byte @NonNull [] getHexEncodedByteArray(byte @NonNull [] aInput)
      Convert a byte array to a hexadecimal encoded byte array.
      Parameters:
      aInput - The byte array to be converted to a String. May not be null.
      Returns:
      The hex encoded byte array representation of the byte array.
      Since:
      12.1.1
    • getHexEncodedByteArray

      public static byte @NonNull [] getHexEncodedByteArray(byte @NonNull [] aInput, int nOfs, int nLen)
      Convert a byte array to a hexadecimal encoded byte array.
      Parameters:
      aInput - The byte array to be converted to a String. May not be null.
      nOfs - Byte array offset
      nLen - Number of bytes to encode
      Returns:
      The hex encoded byte array representation of the byte array.
      Since:
      12.1.1
    • getHexValue

      @CheckForSigned public static int getHexValue(@Nonnegative char c)
      Get the decimal value of the passed hex character
      Parameters:
      c - The hex char to convert (0-9, a-f, A-F)
      Returns:
      A value between 0 and 15, or -1 if the input character is not a hex char!
    • getHexByte

      public static int getHexByte(@Nonnegative char cHigh, @Nonnegative char cLow)
      Parameters:
      cHigh - High hex part
      cLow - Low hex part
      Returns:
      A value between 0 and 255, or -1 if any input character is not a hex char!
    • getHexDecoded

      @ReturnsMutableCopy public static byte @NonNull [] getHexDecoded(@NonNull String sInput)
      Decode a hex encoded string to a byte array.
      Parameters:
      sInput - The hex encoded string. May not be null.
      Returns:
      The decoded byte array. Never null.
    • getHexDecoded

      @ReturnsMutableCopy public static byte @NonNull [] getHexDecoded(char @NonNull [] aInput)
      Decode a hex encoded char array to a byte array.
      Parameters:
      aInput - The hex encoded char array. May not be null.
      Returns:
      The decoded byte array. Never null.
    • getHexDecoded

      @ReturnsMutableCopy public static byte @NonNull [] getHexDecoded(char @NonNull [] aInput, @Nonnegative int nOfs, @Nonnegative int nLen)
      Decode a hex encoded char array to a byte array.
      Parameters:
      aInput - The hex encoded char array. May not be null.
      nOfs - The offset in the char array to start decoding from.
      nLen - The number of chars to decode. Must be an even number.
      Returns:
      The decoded byte array. Never null.
    • getHexDecoded

      @ReturnsMutableCopy public static byte @NonNull [] getHexDecoded(byte @NonNull [] aInput)
      Decode a hex encoded byte array to a byte array.
      Parameters:
      aInput - The hex encoded byte array. May not be null.
      Returns:
      The decoded byte array. Never null.
    • getHexDecoded

      @ReturnsMutableCopy public static byte @NonNull [] getHexDecoded(byte @NonNull [] aInput, @Nonnegative int nOfs, @Nonnegative int nLen)
      Decode a hex encoded byte array to a byte array.
      Parameters:
      aInput - The hex encoded byte array. May not be null.
      nOfs - The offset in the byte array to start decoding from.
      nLen - The number of bytes to decode. Must be an even number.
      Returns:
      The decoded byte array. Never null.
    • getHexString

      public static @NonNull String getHexString(byte nValue)
      Convert a byte value to a hex string. The byte is handled as unsigned.
      Parameters:
      nValue - The byte value to convert.
      Returns:
      The hex string representation of the unsigned byte value.
    • getHexStringLeadingZero

      public static @NonNull String getHexStringLeadingZero(byte nValue, int nDigits)
      Convert a byte value to a hex string with leading zeros up to the specified number of digits.
      Parameters:
      nValue - The byte value to convert.
      nDigits - The minimum number of digits in the output string.
      Returns:
      The hex string representation with leading zeros.
    • getHexStringLeadingZero2

      public static @NonNull String getHexStringLeadingZero2(byte nValue)
      Convert a byte value to a hex string with a leading zero to ensure at least 2 digits. This is a optimized shortcut for getHexStringLeadingZero(nValue, 2).
      Parameters:
      nValue - The byte value to convert.
      Returns:
      The hex string representation with at least 2 digits.
    • getHexString

      public static @NonNull String getHexString(int nValue)
      Convert an int value to a hex string.
      Parameters:
      nValue - The int value to convert.
      Returns:
      The hex string representation of the int value.
    • getHexStringLeadingZero

      public static @NonNull String getHexStringLeadingZero(int nValue, int nDigits)
      Convert an int value to a hex string with leading zeros up to the specified number of digits. Negative values are prefixed with a minus sign.
      Parameters:
      nValue - The int value to convert.
      nDigits - The minimum number of digits in the output string.
      Returns:
      The hex string representation with leading zeros.
    • getHexString

      public static @NonNull String getHexString(long nValue)
      Convert a long value to a hex string.
      Parameters:
      nValue - The long value to convert.
      Returns:
      The hex string representation of the long value.
    • getHexStringLeadingZero

      public static @NonNull String getHexStringLeadingZero(long nValue, int nDigits)
      Convert a long value to a hex string with leading zeros up to the specified number of digits. Negative values are prefixed with a minus sign.
      Parameters:
      nValue - The long value to convert.
      nDigits - The minimum number of digits in the output string.
      Returns:
      The hex string representation with leading zeros.
    • getHexString

      public static @NonNull String getHexString(short nValue)
      Convert a short value to a hex string. The short is handled as unsigned.
      Parameters:
      nValue - The short value to convert.
      Returns:
      The hex string representation of the unsigned short value.
    • getHexStringLeadingZero

      public static @NonNull String getHexStringLeadingZero(short nValue, int nDigits)
      Convert a short value to a hex string with leading zeros up to the specified number of digits. The short is handled as unsigned.
      Parameters:
      nValue - The short value to convert.
      nDigits - The minimum number of digits in the output string.
      Returns:
      The hex string representation with leading zeros.