Package com.helger.base.string
Class StringHex
java.lang.Object
com.helger.base.string.StringHex
Helper class for hexadecimal string encoding and decoding of byte arrays and
individual values.
- Author:
- Philip Helger
-
Method Summary
Modifier and TypeMethodDescriptionstatic intgetHexByte(char cHigh, char cLow) static chargetHexChar(int n) Get the matching hex digit as a lower case character.static chargetHexCharUpperCase(int n) 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 StringgetHexEncoded(byte @NonNull [] aInput) Convert a byte array to a hexadecimal encoded string.static @NonNull StringgetHexEncoded(byte @NonNull [] aInput, int nOfs, int nLen) Convert a byte array to a hexadecimal encoded string.static @NonNull StringgetHexEncoded(@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 StringgetHexString(byte nValue) Convert a byte value to a hex string.static @NonNull StringgetHexString(int nValue) Convert an int value to a hex string.static @NonNull StringgetHexString(long nValue) Convert a long value to a hex string.static @NonNull StringgetHexString(short nValue) Convert a short value to a hex string.static @NonNull StringgetHexStringLeadingZero(byte nValue, int nDigits) Convert a byte value to a hex string with leading zeros up to the specified number of digits.static @NonNull StringgetHexStringLeadingZero(int nValue, int nDigits) Convert an int value to a hex string with leading zeros up to the specified number of digits.static @NonNull StringgetHexStringLeadingZero(long nValue, int nDigits) Convert a long value to a hex string with leading zeros up to the specified number of digits.static @NonNull StringgetHexStringLeadingZero(short nValue, int nDigits) Convert a short value to a hex string with leading zeros up to the specified number of digits.static @NonNull StringgetHexStringLeadingZero2(byte nValue) Convert a byte value to a hex string with a leading zero to ensure at least 2 digits.static intgetHexValue(char c) Get the decimal value of the passed hex character
-
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
Convert a string to a byte array and than to a hexadecimal encoded string.- Parameters:
sInput- The source string. May not benull.aCharset- The charset to use. May not benull.- Returns:
- The String representation of the byte array of the string.
-
getHexEncoded
Convert a byte array to a hexadecimal encoded string.- Parameters:
aInput- The byte array to be converted to a String. May not benull.- Returns:
- The String representation of the byte array.
-
getHexEncoded
Convert a byte array to a hexadecimal encoded string.- Parameters:
aInput- The byte array to be converted to a String. May not benull.nOfs- Byte array offsetnLen- 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 benull.- 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 benull.nOfs- Byte array offsetnLen- 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 partcLow- Low hex part- Returns:
- A value between 0 and 255, or -1 if any input character is not a hex char!
-
getHexDecoded
Decode a hex encoded string to a byte array.- Parameters:
sInput- The hex encoded string. May not benull.- 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 benull.- 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 benull.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 benull.- 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 benull.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
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
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
Convert a byte value to a hex string with a leading zero to ensure at least 2 digits. This is a optimized shortcut forgetHexStringLeadingZero(nValue, 2).- Parameters:
nValue- The byte value to convert.- Returns:
- The hex string representation with at least 2 digits.
-
getHexString
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
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
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
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
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
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.
-