Package edu.rit.util
Class Hex
java.lang.Object
edu.rit.util.Hex
Class Hex provides static methods for converting between hexadecimal strings
and numbers of various kinds.
- Version:
- 22-Apr-2006
- Author:
- Alan Kaminsky
-
Method Summary
Modifier and TypeMethodDescriptionstatic byteConvert the given hexadecimal string to abytevalue.static byte[]toByteArray(String str) Convert the given hexadecimal string to a sequence of bytes stored in a newbytearray.static voidtoByteArray(String str, byte[] val) Convert the given hexadecimal string to a sequence of bytes stored in the givenbytearray.static voidtoByteArray(String str, byte[] val, int off, int len) Convert the given hexadecimal string to a sequence of bytes stored in a portion of the givenbytearray.static charConvert the given hexadecimal string to acharvalue.static intConvert the given hexadecimal string to anintvalue.static longConvert the given hexadecimal string to alongvalue.static shortConvert the given hexadecimal string to ashortvalue.static StringtoString(byte val) Convert the givenbytevalue to a two-digit hexadecimal string.static StringtoString(byte[] val) Convert the givenbytearray to a hexadecimal string.static StringtoString(byte[] val, int off, int len) Convert a portion of the givenbytearray to a hexadecimal string.static StringtoString(char val) Convert the givencharvalue to a four-digit hexadecimal string.static StringtoString(int val) Convert the givenintvalue to an eight-digit hexadecimal string.static StringtoString(long val) Convert the givenlongvalue to a sixteen-digit hexadecimal string.static StringtoString(short val) Convert the givenshortvalue to a four-digit hexadecimal string.
-
Method Details
-
toString
Convert the givenbytevalue to a two-digit hexadecimal string.- Parameters:
val- Value.- Returns:
- Hexadecimal string.
-
toString
Convert the givencharvalue to a four-digit hexadecimal string.- Parameters:
val- Value.- Returns:
- Hexadecimal string.
-
toString
Convert the givenshortvalue to a four-digit hexadecimal string.- Parameters:
val- Value.- Returns:
- Hexadecimal string.
-
toString
Convert the givenintvalue to an eight-digit hexadecimal string.- Parameters:
val- Value.- Returns:
- Hexadecimal string.
-
toString
Convert the givenlongvalue to a sixteen-digit hexadecimal string.- Parameters:
val- Value.- Returns:
- Hexadecimal string.
-
toString
Convert the givenbytearray to a hexadecimal string. Each byte is converted to two hexadecimal digits.- Parameters:
val- Byte array.- Returns:
- Hexadecimal string.
- Throws:
NullPointerException- (unchecked exception) Thrown ifvalis null.
-
toString
Convert a portion of the givenbytearray to a hexadecimal string. Each byte is converted to two hexadecimal digits.- Parameters:
val- Byte array.off- Index of first byte to convert.len- Number of bytes to convert.- Returns:
- Hexadecimal string.
- Throws:
NullPointerException- (unchecked exception) Thrown ifvalis null.IndexOutOfBoundsException- (unchecked exception) Thrown ifoff< 0,len< 0, oroff+len>val.length.
-
toByte
Convert the given hexadecimal string to abytevalue. Characters of the string from the highest index to the lowest index give the hexadecimal digits of the value from least significant digit to most significant digit. Any extra high-order digits in the string are omitted.- Parameters:
str- Hexadecimal string.- Returns:
- Value.
- Throws:
NullPointerException- (unchecked exception) Thrown ifstris null.IllegalArgumentException- (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0'..'9','a'..'f', or'A'..'F').
-
toChar
Convert the given hexadecimal string to acharvalue. Characters of the string from the highest index to the lowest index give the hexadecimal digits of the value from least significant digit to most significant digit. Any extra high-order digits in the string are omitted.- Parameters:
str- Hexadecimal string.- Returns:
- Value.
- Throws:
NullPointerException- (unchecked exception) Thrown ifstris null.IllegalArgumentException- (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0'..'9','a'..'f', or'A'..'F').
-
toShort
Convert the given hexadecimal string to ashortvalue. Characters of the string from the highest index to the lowest index give the hexadecimal digits of the value from least significant digit to most significant digit. Any extra high-order digits in the string are omitted.- Parameters:
str- Hexadecimal string.- Returns:
- Value.
- Throws:
NullPointerException- (unchecked exception) Thrown ifstris null.IllegalArgumentException- (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0'..'9','a'..'f', or'A'..'F').
-
toInt
Convert the given hexadecimal string to anintvalue. Characters of the string from the highest index to the lowest index give the hexadecimal digits of the value from least significant digit to most significant digit. Any extra high-order digits in the string are omitted.- Parameters:
str- Hexadecimal string.- Returns:
- Value.
- Throws:
NullPointerException- (unchecked exception) Thrown ifstris null.IllegalArgumentException- (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0'..'9','a'..'f', or'A'..'F').
-
toLong
Convert the given hexadecimal string to alongvalue. Characters of the string from the highest index to the lowest index give the hexadecimal digits of the value from least significant digit to most significant digit. Any extra high-order digits in the string are omitted.- Parameters:
str- Hexadecimal string.- Returns:
- Value.
- Throws:
NullPointerException- (unchecked exception) Thrown ifstris null.IllegalArgumentException- (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0'..'9','a'..'f', or'A'..'F').
-
toByteArray
Convert the given hexadecimal string to a sequence of bytes stored in a newbytearray. Characters of the string from the highest index to the lowest index give the hexadecimal digits of the value from least significant digit to most significant digit. The value is stored in a newly allocated byte array, with the least significant byte stored at the highest index and the most significant byte stored at the lowest index. The array's length is sufficient to hold the entire converted string. The newly allocated byte array is returned.- Parameters:
str- Hexadecimal string.- Returns:
- Value (byte array).
- Throws:
NullPointerException- (unchecked exception) Thrown ifstris null.IllegalArgumentException- (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0'..'9','a'..'f', or'A'..'F').
-
toByteArray
Convert the given hexadecimal string to a sequence of bytes stored in the givenbytearray. Characters of the string from the highest index to the lowest index give the hexadecimal digits of the value from least significant digit to most significant digit. The value is stored invalfrom highest index to lowest index, with the least significant byte stored at indexval.length-1. If the converted string requires more thanval.lengthbytes, the extra digits at the beginning of the string are not converted. If the converted string requires fewer thanval.lengthbytes, the extra bytes at the beginning ofvalare set to 0.- Parameters:
str- Hexadecimal string.val- Byte array in which to store converted value.- Throws:
NullPointerException- (unchecked exception) Thrown ifstris null orvalis null.IllegalArgumentException- (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0'..'9','a'..'f', or'A'..'F').
-
toByteArray
Convert the given hexadecimal string to a sequence of bytes stored in a portion of the givenbytearray. Characters of the string from the highest index to the lowest index give the hexadecimal digits of the value from least significant digit to most significant digit. The value is stored invalfrom highest index to lowest index, with the least significant byte stored at indexoff+len-1. If the converted string requires more thanlenbytes, the extra digits at the beginning of the string are not converted. If the converted string requires fewer thanlenbytes, the extra bytes starting at indexoffofvalare set to 0.- Parameters:
str- Hexadecimal string.val- Byte array in which to store converted value.off- Index of first byte to store.len- Number of bytes to store.- Throws:
NullPointerException- (unchecked exception) Thrown ifstris null orvalis null.IllegalArgumentException- (unchecked exception) Thrown ifoff< 0,len< 0, oroff+len>val.length. Thrown if any character of the string is not a hexadecimal digit ('0'..'9','a'..'f', or'A'..'F').
-