Class HexUtil
java.lang.Object
de.tomatengames.util.HexUtil
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringbytesToHex(byte[] bytes) Converts the specified byte array into a hexadecimal string.static StringbyteToHex(int value) Converts the 8 least significant bits of the specifiedintinto a hexadecimal string.static StringReads the specified file and returns its content as a hex string.static byte[]hexToBytes(String hexString) Reads the specified hexadecimal string into a byte array.static intReads the specified hexadecimal string into anint.static longReads the specified hexadecimal string into along.static StringintToHex(int value) Converts the bits of the specifiedintinto a hexadecimal string.static StringlongToHex(long value) Converts the bits of the specifiedlonginto a hexadecimal string.static intparseHexChar(char hexChar) Returns the value of the specified hexadecimal character.static StringshortToHex(int value) Converts the 16 least significant bits of the specifiedintinto a hexadecimal string.static StringstreamToHex(InputStream input) Reads the specifiedInputStreamand returns its content as a hex string.static chartoHexChar(int value) Returns the hexadecimal character that represents the specified value.
-
Method Details
-
toHexChar
public static char toHexChar(int value) Returns the hexadecimal character that represents the specified value.- Parameters:
value- The value (0-15).- Returns:
- The hexadecimal character (0-9, a-f).
- Throws:
IllegalArgumentException- If the value is out of range.
-
bytesToHex
Converts the specified byte array into a hexadecimal string. Each byte is represented by 2 characters.- Parameters:
bytes- The byte array.- Returns:
- The hexadecimal string. Not
null. Alphabetic characters are in lower case. - Throws:
NullPointerException- If the byte array isnull.
-
longToHex
Converts the bits of the specifiedlonginto a hexadecimal string. The first character represents the most significant bits.- Parameters:
value- Thelong.- Returns:
- The hexadecimal string. Not
null. The length is always16. Alphabetic characters are in lower case.
-
intToHex
Converts the bits of the specifiedintinto a hexadecimal string. The first character represents the most significant bits.- Parameters:
value- Theint.- Returns:
- The hexadecimal string. Not
null. The length is always8. Alphabetic characters are in lower case.
-
byteToHex
Converts the 8 least significant bits of the specifiedintinto a hexadecimal string. The other bits are ignored. The first character of the result represents the most significant bits.- Parameters:
value- Theintvalue.- Returns:
- The hexadecimal string. Not
null. The length is always2. Alphabetic characters are in lower case. - Since:
- 1.6
-
shortToHex
Converts the 16 least significant bits of the specifiedintinto a hexadecimal string. The other bits are ignored. The first character of the result represents the most significant bits.- Parameters:
value- Theintvalue.- Returns:
- The hexadecimal string. Not
null. The length is always4. Alphabetic characters are in lower case. - Since:
- 1.6
-
streamToHex
Reads the specifiedInputStreamand returns its content as a hex string. Each byte of the input is represented by 2 characters.- Parameters:
input- The input stream.- Returns:
- The hexadecimal string. Not
null. Alphabetic characters are in lower case. - Throws:
IOException- If an I/O error occurs.NullPointerException- If the input stream isnull.
-
fileToHex
Reads the specified file and returns its content as a hex string. Each byte of the file is represented by 2 characters.- Parameters:
path- The path to the file.- Returns:
- The hexadecimal string. Not
null. Alphabetic characters are in lower case. - Throws:
IOException- If an I/O error occurs.NullPointerException- If the path isnull.
-
parseHexChar
public static int parseHexChar(char hexChar) Returns the value of the specified hexadecimal character.- Parameters:
hexChar- The hexadecimal character (0-9, a-f, A-F).- Returns:
- The value that the character represents (0-15). It is guaranteed that only the 4 least significant bits may be set.
- Throws:
IllegalArgumentException- If the character is not a hex character.
-
hexToBytes
Reads the specified hexadecimal string into a byte array.Two characters of the string are read into one byte. The first hex char represents the 4 most significant bits and the second hex char the 4 least significant bits of the byte. If the length of the hex string is odd, the 4 least significant bits of the last byte are
0000.- Parameters:
hexString- The hexadecimal string.- Returns:
- The byte array that represents the hexadecimal string. Not
null. - Throws:
NullPointerException- If the input string isnull.IllegalArgumentException- If the string contains a non-hexadecimal character.
-
hexToLong
Reads the specified hexadecimal string into along. Each character represents 4 bits of the result. The last character of the string represents the least significant bits.Note: A
'-'sign is not allowed.- Parameters:
hexString- The hexadecimal string. Must not benull. Must not be larger than 16 characters.- Returns:
- A
longthat represents the hex string. - Throws:
NullPointerException- If the hex string isnull.IllegalArgumentException- If the hex string is too large or contains non-hexadecimal characters.
-
hexToInt
Reads the specified hexadecimal string into anint. Each character represents 4 bits of the result. The last character of the string represents the least significant bits.Note: A
'-'sign is not allowed.- Parameters:
hexString- The hexadecimal string. Must not benull. Must not be larger than 8 characters.- Returns:
- An
intthat represents the hex string. - Throws:
NullPointerException- If the hex string isnull.IllegalArgumentException- If the hex string is too large or contains non-hexadecimal characters.- Since:
- 1.1
-