edu.rit.util
Class Hex
- java.lang.Object
-
- edu.rit.util.Hex
-
public class Hex extends java.lang.ObjectClass Hex provides static methods for converting between hexadecimal strings and numbers of various kinds.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static bytetoByte(java.lang.String str)Convert the given hexadecimal string to a byte value.static byte[]toByteArray(java.lang.String str)Convert the given hexadecimal string to a sequence of bytes stored in a new byte array.static voidtoByteArray(java.lang.String str, byte[] val)Convert the given hexadecimal string to a sequence of bytes stored in the given byte array.static voidtoByteArray(java.lang.String str, byte[] val, int off, int len)Convert the given hexadecimal string to a sequence of bytes stored in a portion of the given byte array.static chartoChar(java.lang.String str)Convert the given hexadecimal string to a char value.static inttoInt(java.lang.String str)Convert the given hexadecimal string to an int value.static longtoLong(java.lang.String str)Convert the given hexadecimal string to a long value.static shorttoShort(java.lang.String str)Convert the given hexadecimal string to a short value.static java.lang.StringtoString(byte val)Convert the given byte value to a two-digit hexadecimal string.static java.lang.StringtoString(byte[] val)Convert the given byte array to a hexadecimal string.static java.lang.StringtoString(byte[] val, int off, int len)Convert a portion of the given byte array to a hexadecimal string.static java.lang.StringtoString(char val)Convert the given char value to a four-digit hexadecimal string.static java.lang.StringtoString(int val)Convert the given int value to an eight-digit hexadecimal string.static java.lang.StringtoString(long val)Convert the given long value to a sixteen-digit hexadecimal string.static java.lang.StringtoString(short val)Convert the given short value to a four-digit hexadecimal string.
-
-
-
Method Detail
-
toString
public static java.lang.String toString(byte val)
Convert the given byte value to a two-digit hexadecimal string.- Parameters:
val- Value.- Returns:
- Hexadecimal string.
-
toString
public static java.lang.String toString(char val)
Convert the given char value to a four-digit hexadecimal string.- Parameters:
val- Value.- Returns:
- Hexadecimal string.
-
toString
public static java.lang.String toString(short val)
Convert the given short value to a four-digit hexadecimal string.- Parameters:
val- Value.- Returns:
- Hexadecimal string.
-
toString
public static java.lang.String toString(int val)
Convert the given int value to an eight-digit hexadecimal string.- Parameters:
val- Value.- Returns:
- Hexadecimal string.
-
toString
public static java.lang.String toString(long val)
Convert the given long value to a sixteen-digit hexadecimal string.- Parameters:
val- Value.- Returns:
- Hexadecimal string.
-
toString
public static java.lang.String toString(byte[] val)
Convert the given byte array to a hexadecimal string. Each byte is converted to two hexadecimal digits.- Parameters:
val- Byte array.- Returns:
- Hexadecimal string.
- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if val is null.
-
toString
public static java.lang.String toString(byte[] val, int off, int len)Convert a portion of the given byte array 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:
java.lang.NullPointerException- (unchecked exception) Thrown if val is null.java.lang.IndexOutOfBoundsException- (unchecked exception) Thrown if off < 0, len < 0, or off+len > val.length.
-
toByte
public static byte toByte(java.lang.String str)
Convert the given hexadecimal string to a byte value. 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:
java.lang.NullPointerException- (unchecked exception) Thrown if str is null.java.lang.IllegalArgumentException- (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0' .. '9', 'a' .. 'f', or 'A' .. 'F').
-
toChar
public static char toChar(java.lang.String str)
Convert the given hexadecimal string to a char value. 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:
java.lang.NullPointerException- (unchecked exception) Thrown if str is null.java.lang.IllegalArgumentException- (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0' .. '9', 'a' .. 'f', or 'A' .. 'F').
-
toShort
public static short toShort(java.lang.String str)
Convert the given hexadecimal string to a short value. 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:
java.lang.NullPointerException- (unchecked exception) Thrown if str is null.java.lang.IllegalArgumentException- (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0' .. '9', 'a' .. 'f', or 'A' .. 'F').
-
toInt
public static int toInt(java.lang.String str)
Convert the given hexadecimal string to an int value. 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:
java.lang.NullPointerException- (unchecked exception) Thrown if str is null.java.lang.IllegalArgumentException- (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0' .. '9', 'a' .. 'f', or 'A' .. 'F').
-
toLong
public static long toLong(java.lang.String str)
Convert the given hexadecimal string to a long value. 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:
java.lang.NullPointerException- (unchecked exception) Thrown if str is null.java.lang.IllegalArgumentException- (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0' .. '9', 'a' .. 'f', or 'A' .. 'F').
-
toByteArray
public static byte[] toByteArray(java.lang.String str)
Convert the given hexadecimal string to a sequence of bytes stored in a new byte array. 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:
java.lang.NullPointerException- (unchecked exception) Thrown if str is null.java.lang.IllegalArgumentException- (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0' .. '9', 'a' .. 'f', or 'A' .. 'F').
-
toByteArray
public static void toByteArray(java.lang.String str, byte[] val)Convert the given hexadecimal string to a sequence of bytes stored in the given byte array. 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 val from highest index to lowest index, with the least significant byte stored at index val.length-1. If the converted string requires more than val.length bytes, the extra digits at the beginning of the string are not converted. If the converted string requires fewer than val.length bytes, the extra bytes at the beginning of val are set to 0.- Parameters:
str- Hexadecimal string.val- Byte array in which to store converted value.- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if str is null or val is null.java.lang.IllegalArgumentException- (unchecked exception) Thrown if any character of the string is not a hexadecimal digit ('0' .. '9', 'a' .. 'f', or 'A' .. 'F').
-
toByteArray
public static void toByteArray(java.lang.String str, byte[] val, int off, int len)Convert the given hexadecimal string to a sequence of bytes stored in a portion of the given byte array. 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 val from highest index to lowest index, with the least significant byte stored at index off+len-1. If the converted string requires more than len bytes, the extra digits at the beginning of the string are not converted. If the converted string requires fewer than len bytes, the extra bytes starting at index off of val are 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:
java.lang.NullPointerException- (unchecked exception) Thrown if str is null or val is null.java.lang.IllegalArgumentException- (unchecked exception) Thrown if off < 0, len < 0, or off+len > val.length. Thrown if any character of the string is not a hexadecimal digit ('0' .. '9', 'a' .. 'f', or 'A' .. 'F').
-
-
DMelt 3.0 © DataMelt by jWork.ORG