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 byte
Convert the given hexadecimal string to abyte
value.static byte[]
toByteArray
(String str) Convert the given hexadecimal string to a sequence of bytes stored in a newbyte
array.static void
toByteArray
(String str, byte[] val) Convert the given hexadecimal string to a sequence of bytes stored in the givenbyte
array.static void
toByteArray
(String str, byte[] val, int off, int len) Convert the given hexadecimal string to a sequence of bytes stored in a portion of the givenbyte
array.static char
Convert the given hexadecimal string to achar
value.static int
Convert the given hexadecimal string to anint
value.static long
Convert the given hexadecimal string to along
value.static short
Convert the given hexadecimal string to ashort
value.static String
toString
(byte val) Convert the givenbyte
value to a two-digit hexadecimal string.static String
toString
(byte[] val) Convert the givenbyte
array to a hexadecimal string.static String
toString
(byte[] val, int off, int len) Convert a portion of the givenbyte
array to a hexadecimal string.static String
toString
(char val) Convert the givenchar
value to a four-digit hexadecimal string.static String
toString
(int val) Convert the givenint
value to an eight-digit hexadecimal string.static String
toString
(long val) Convert the givenlong
value to a sixteen-digit hexadecimal string.static String
toString
(short val) Convert the givenshort
value to a four-digit hexadecimal string.
-
Method Details
-
toString
Convert the givenbyte
value to a two-digit hexadecimal string.- Parameters:
val
- Value.- Returns:
- Hexadecimal string.
-
toString
Convert the givenchar
value to a four-digit hexadecimal string.- Parameters:
val
- Value.- Returns:
- Hexadecimal string.
-
toString
Convert the givenshort
value to a four-digit hexadecimal string.- Parameters:
val
- Value.- Returns:
- Hexadecimal string.
-
toString
Convert the givenint
value to an eight-digit hexadecimal string.- Parameters:
val
- Value.- Returns:
- Hexadecimal string.
-
toString
Convert the givenlong
value to a sixteen-digit hexadecimal string.- Parameters:
val
- Value.- Returns:
- Hexadecimal string.
-
toString
Convert the givenbyte
array to a hexadecimal string. Each byte is converted to two hexadecimal digits.- Parameters:
val
- Byte array.- Returns:
- Hexadecimal string.
- Throws:
NullPointerException
- (unchecked exception) Thrown ifval
is null.
-
toString
Convert a portion of the givenbyte
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:
NullPointerException
- (unchecked exception) Thrown ifval
is null.IndexOutOfBoundsException
- (unchecked exception) Thrown ifoff
< 0,len
< 0, oroff+len
>val.length
.
-
toByte
Convert the given hexadecimal string to abyte
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:
NullPointerException
- (unchecked exception) Thrown ifstr
is 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 achar
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:
NullPointerException
- (unchecked exception) Thrown ifstr
is 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 ashort
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:
NullPointerException
- (unchecked exception) Thrown ifstr
is 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 anint
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:
NullPointerException
- (unchecked exception) Thrown ifstr
is 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 along
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:
NullPointerException
- (unchecked exception) Thrown ifstr
is 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 newbyte
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:
NullPointerException
- (unchecked exception) Thrown ifstr
is 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 givenbyte
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 inval
from highest index to lowest index, with the least significant byte stored at indexval.length-1
. If the converted string requires more thanval.length
bytes, the extra digits at the beginning of the string are not converted. If the converted string requires fewer thanval.length
bytes, the extra bytes at the beginning ofval
are set to 0.- Parameters:
str
- Hexadecimal string.val
- Byte array in which to store converted value.- Throws:
NullPointerException
- (unchecked exception) Thrown ifstr
is null orval
is 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 givenbyte
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 inval
from highest index to lowest index, with the least significant byte stored at indexoff+len-1
. If the converted string requires more thanlen
bytes, the extra digits at the beginning of the string are not converted. If the converted string requires fewer thanlen
bytes, the extra bytes starting at indexoff
ofval
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:
NullPointerException
- (unchecked exception) Thrown ifstr
is null orval
is 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'
).
-