Package de.das.encrypter.tools
Class HexTool
java.lang.Object
de.das.encrypter.tools.HexTool
Methods for handling conversions of data types from and to byte arrays.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic float[]byteArrayToFloatArray(byte[] byteArray) Takes every 4 bytes of the byte array consecutively, converts it to a float value and places it to its correct place into the float array.static longbyteArrayToLong(byte[] b) Converts the specified byte array, assumed to be a little endian, to a long value.static StringbyteArrayToString(byte[] data) Generates a string consisting of the string representations of the hexadecimal values of the given ByteArray separated by spaces.static StringbyteArrayToString(byte[] data, int blockSize) Generates a string consisting of the string representations of the hexadecimal values of the given ByteArray separated by spaces.static intbyteToInt(byte b) Converts a byte to integer.static intbyteToInt(byte[] bytes) Converts the specified byte array, assumed to be a little endian, to a long value.static intbyteToInt(int msb, int lsb) Generates an integer value by composing it from the specified parts (msb and lsb).static intbyteToInt(int msb, int csb, int lsb) Generates an integer value by composing it from the specified parts (msb, csb and lsb).static intbyteToIntBigEndian(int pos, byte[] bytes) Converts an four-byte long part from the specified byte array starting at the specified position, assuming it is a big-endian value, to an integer value.static intbyteToIntLittleEndian(int pos, byte[] bytes) Converts an four-byte long part from the specified byte array starting at the specified position, assuming it is a little-endian value, to an integer value.static longbyteToLongBigEndian(byte[] bytes, int pos) Converts an eight-byte long part from the specified byte array starting at the specified position, assuming it is a big-endian value, to a long-int value.static longbyteToLongLittleEndian(byte[] bytes) Converts the specified byte array, assumed to be a little endian, to a long int value.static longbyteToLongLittleEndian(byte[] bytes, int pos) Converts an eight-byte long part from the specified byte array starting at the specified position, assuming it is a little-endian value, to a long-int value.static shortbyteToShortIntBigEndian(int pos, byte[] bytes) Converts an two-byte long part from the specified byte array starting at the specified position, assuming it is a big-endian value, to a short integer value.static shortbyteToShortIntLittleEndian(int pos, byte[] bytes) Converts an two-byte long part from the specified byte array starting at the specified position, assuming it is a little-endian value, to a short int value.static StringbyteToString(byte[] data, int start, int length) Creates a string using the specified part of the given byte array.static longbyteToUintBigEndian(int pos, byte[] bytes) Converts an four-byte long part from the specified byte array starting at the specified position, assuming it is a big-endian value, to an unsigned integer value.static longbyteToUintLittleEndian(int pos, byte[] bytes) Converts an four-byte long part from the specified byte array starting at the specified position, assuming it is a little-endian value, to an unsigned integer value.static shortbyteToWord(int msb, int lsb) Generates a short integer value by composing it from the specified parts (msb and lsb).static intbyteToWordBigEndian(int pos, byte[] bytes) Converts an two-byte long part from the specified byte array starting at the specified position, assuming it is a big-endian value, to an integer value.static intbyteToWordLittleEndian(int pos, byte[] bytes) Converts an two-byte long part from the specified byte array starting at the specified position, assuming it is a little-endian value, to an integer value.static StringcreateFormattedHexList(byte[] data) Generates a string representing a formatted hex list with sixteen bytes per line and a preceding eight-character hexadecimal address.static StringcreateFormattedHexList(byte[] data, String centerDiv) Generates a string representing a formatted hex list with sixteen bytes per line and a preceding eight-character hexadecimal address.static StringcreateFormattedHexList(int adr, byte[] data, String centerDiv) Generates a string representing a formatted hex list with sixteen bytes per line and a preceding eight-character hexadecimal address.static StringfillLeadingPositions(String s, int n, char c) Expands the string "s" if its length is smaller than the given length "n" by adding leading characters "c".static byte[]floatArrayToByteArray(float[] fArray) Maps the 4 bytes of each float value to its correct place into the byte array.static byte[]floatToByteArray(float f) Assigns the four bytes representing a floating point value to a byte array with bit 31 of the floating point at bit 7 of byte at position "0".static floatgetFloatAt(int pos, byte[] data) Converts the four bytes at the given starting position to a floating point value assuming the byte order as little endian.static StringgetHexString(byte[] data) Converts the byte values of the byte array into hex strings and arranges them one after another separated by spaces.static intgetIntAt(int pos, byte[] data) Converts the four bytes at the given starting position to an integer value assuming as little endian.static longgetLongAt(int pos, byte[] data) Converts the eight bytes at the given starting position to a long value assuming as little endian.static voidinsertFloatAt(int pos, float fValue, byte[] data) Converts a the float four bytes representation into an integer and places the four bytes of the integer into the byte array from the given position on.static voidinsertIntAt(int v, int pos, byte[] data) Assigns the four bytes of an integer value to the bytes of the given byte array starting at the specified position with byte order big endian.static voidinsertIntAt(long v, int pos, byte[] data) Deprecated.Use insertIntBigEndianAt instead.static voidinsertIntBigEndianAt(long v, int pos, byte[] data) Assigns the four lsb bytes of a long value to the bytes of the given byte array starting at the specified position with byte order big endian.static voidinsertIntLittleEndianAt(long v, int pos, byte[] data) Assigns the four lsb bytes of a long value to the bytes of the given byte array starting at the specified position with byte order little endian.static voidinsertLongLittleEndianAt(long v, int pos, byte[] data) Assigns the eight bytes of a long value to the bytes of the given byte array starting at the specified position with byte order little endian.static voidinsertShortIntAt(int v, int pos, byte[] data) Deprecated.Use insertShortIntBigEndianAt instead.static voidinsertShortIntBigEndianAt(int v, int pos, byte[] data) Assigns the two lsb bytes of an int value to the bytes of the given byte array starting at the specified position with byte order big endian.static voidinsertShortIntLittleEndianAt(int v, int pos, byte[] data) Assigns the two lsb bytes of an int value to the bytes of the given byte array starting at the specified position with byte order little endian.static byte[]intToByteArray(int v) Converts the given int value into a byte array of length 4.static byte[]longToByteArray(long v) Deprecated.Use "longToByteArrayLittleEndian" instead.static byte[]longToByteArrayBigEndian(long v) Converts the given long value into a byte array of length 8.static byte[]longToByteArrayLittleEndian(long v) Converts the given long value into a byte array of length 8.static byteConverts a string of length two, assumed to be the representation of a hex value, to a byte.static shortparseQuadHex(String str) Converts a string of length four, assumed to be the representation of a hex value, to a byte.static voidprintBuffer(byte[] data) Converts the specified byte array to a hex string and outputs it over the System.out print stream.static voidprintBuffer(PrintStream out, byte[] data) Converts the specified byte array to a hex string and outputs it over the specified print stream.static voidprintlnBuffer(byte[] data) Converts the specified byte array to a hex string and outputs it over the System.out print stream followed by a System.out.println().static StringstringFromByteArray(byte[] b) Creates a string from the bytes of a byte array.static byte[]stringToByteArray(String str) Creates a byte array with the bytes representing the characters of the specified string.static byte[]swapBytes(byte[] data) Reverses the order of the bytes in a byte array.static StringtoHex(byte b) Creates a two character string representing the given byte value.static StringtoHexQuad(short sh) Creates a four character string representing the given short value.static StringtoTwoHexQuad(int i) Creates an eight character string representing the given integer value.
-
Constructor Details
-
HexTool
public HexTool()
-
-
Method Details
-
byteArrayToFloatArray
public static float[] byteArrayToFloatArray(byte[] byteArray) Takes every 4 bytes of the byte array consecutively, converts it to a float value and places it to its correct place into the float array.- Parameters:
byteArray- is a given array with bytes representing float values.- Returns:
- a float array containing the the float values converted from the byte array.
-
byteToInt
public static int byteToInt(byte b) Converts a byte to integer. Unlike simple casting, which would yield the range [-128 .. 127], this method yields the range [0 .. 255].- Parameters:
b- a byte value.- Returns:
- an integer value in the range [0 .. 255].
-
floatArrayToByteArray
public static byte[] floatArrayToByteArray(float[] fArray) Maps the 4 bytes of each float value to its correct place into the byte array. The resulting byte array will have the size (4 * fArray.length).- Parameters:
fArray- is a given array with float values.- Returns:
- a byte array containing the bytes of the float values from the float array.
-
floatToByteArray
public static byte[] floatToByteArray(float f) Assigns the four bytes representing a floating point value to a byte array with bit 31 of the floating point at bit 7 of byte at position "0".
Bit 31 (the bit that is selected by the mask 0x80000000) represents the sign of the floating-point number.
Bits 30-23 (the bits that are selected by the mask 0x7f800000) represent the exponent.
Bits 22-0 (the bits that are selected by the mask 0x007fffff) represent the significand (sometimes called the mantissa) of the floating-point number .
- If the argument is positive infinity, the result is 0x7f800000.
- If the argument is negative infinity, the result is 0xff800000.
- If the argument is NaN, the result is 0x7fc00000.- Parameters:
f- a float value.- Returns:
- a byte array with the four float bytes.
-
longToByteArray
public static byte[] longToByteArray(long v) Deprecated.Use "longToByteArrayLittleEndian" instead.Converts the given long value into a byte array of length 8. The msb is at position 7 and the lsb at position 0.- Parameters:
v- is the given long value.- Returns:
- a byte array of length 8 containing the bytes of the long value.
-
longToByteArrayLittleEndian
public static byte[] longToByteArrayLittleEndian(long v) Converts the given long value into a byte array of length 8. The msb is at position 7 and the lsb at position 0.- Parameters:
v- is the given long value.- Returns:
- a byte array of length 8 containing the bytes of the long value.
-
longToByteArrayBigEndian
public static byte[] longToByteArrayBigEndian(long v) Converts the given long value into a byte array of length 8. The msb is at position 7 and the lsb at position 0.- Parameters:
v- is the given long value.- Returns:
- a byte array of length 8 containing the bytes of the long value.
-
intToByteArray
public static byte[] intToByteArray(int v) Converts the given int value into a byte array of length 4. The msb is at position 3 and the lsb at position 0 (little endian).- Parameters:
v- is the given int value.- Returns:
- a byte array of length 4 containing the bytes of the int value.
-
byteArrayToLong
public static long byteArrayToLong(byte[] b) Converts the specified byte array, assumed to be a little endian, to a long value.- Parameters:
b- a byte array representing a long value.- Returns:
- a long value from the given byte array.
-
stringFromByteArray
Creates a string from the bytes of a byte array. If the byte array contains 0x00 values, only the values up to the position of the first occurrence of a 0x00 value are used.- Parameters:
b- a given byte array.- Returns:
- a string built from the bytes of the given byte array.
-
stringToByteArray
Creates a byte array with the bytes representing the characters of the specified string. The method is equivalent to the "getBytes()" method of the String class. UTF-8 character set is used for conversion.- Parameters:
str- a given string.- Returns:
- a byte array with bytes representing the characters of the string.
-
toHex
Creates a two character string representing the given byte value.- Parameters:
b- a given byte value.- Returns:
- the string representation of the hex value [00 .. FF]
-
toTwoHexQuad
Creates an eight character string representing the given integer value.- Parameters:
i- a given integer value.- Returns:
- the string representation of the hex value [00000000 .. FFFFFFFF]
-
toHexQuad
Creates a four character string representing the given short value.- Parameters:
sh- a given short value.- Returns:
- the string representation of the hex value [0000 .. FFFF]
-
parseHex
Converts a string of length two, assumed to be the representation of a hex value, to a byte. Allowed characters: [0 .. 9, A .. F, a .. f].- Parameters:
str- a string representing a hex value [00 .. FF].- Returns:
- the converted byte value.
- Throws:
NumberFormatException- if one of the characters is not allowed.
-
fillLeadingPositions
Expands the string "s" if its length is smaller than the given length "n" by adding leading characters "c".- Parameters:
s- the given string.n- the requested length.c- the character with which the string is filled.- Returns:
- the expanded string.
-
parseQuadHex
Converts a string of length four, assumed to be the representation of a hex value, to a byte. Allowed characters: [0 .. 9, A .. F, a .. f].- Parameters:
str- a string representing a hex value [0000 .. FFFF].- Returns:
- the converted byte value.
- Throws:
NumberFormatException- if one of the characters is not allowed.
-
byteToWord
public static short byteToWord(int msb, int lsb) Generates a short integer value by composing it from the specified parts (msb and lsb).- Parameters:
msb- the msb part of the short int value.lsb- the lsb part of the short int value.- Returns:
- a short integer value by composing it from the specified parts.
-
byteToInt
public static int byteToInt(int msb, int lsb) Generates an integer value by composing it from the specified parts (msb and lsb).- Parameters:
msb- the msb part of the int value.lsb- the lsb part of the int value.- Returns:
- an integer value by composing it from the specified parts.
-
byteToInt
public static int byteToInt(int msb, int csb, int lsb) Generates an integer value by composing it from the specified parts (msb, csb and lsb).- Parameters:
msb- the msb part of the int value.csb- the csb part of the int value.lsb- the lsb part of the int value.- Returns:
- an integer value by composing it from the specified parts.
-
byteToInt
public static int byteToInt(byte[] bytes) Converts the specified byte array, assumed to be a little endian, to a long value.- Parameters:
bytes- a byte array representing a integer value.- Returns:
- an integer value from the given byte array.
-
byteToLongLittleEndian
public static long byteToLongLittleEndian(byte[] bytes) Converts the specified byte array, assumed to be a little endian, to a long int value.- Parameters:
bytes- a byte array representing a long int value.- Returns:
- a long int value from the given byte array.
-
byteToLongLittleEndian
public static long byteToLongLittleEndian(byte[] bytes, int pos) Converts an eight-byte long part from the specified byte array starting at the specified position, assuming it is a little-endian value, to a long-int value.- Parameters:
bytes- a given byte array.pos- the position within the byte array where the eight byte long part starts.- Returns:
- a long int value from the given byte array at the specified positon.
-
byteToIntLittleEndian
public static int byteToIntLittleEndian(int pos, byte[] bytes) Converts an four-byte long part from the specified byte array starting at the specified position, assuming it is a little-endian value, to an integer value.- Parameters:
bytes- a given byte array.pos- the position within the byte array where the four byte long part starts.- Returns:
- an integer value from the given byte array at the specified positon.
-
byteToLongBigEndian
public static long byteToLongBigEndian(byte[] bytes, int pos) Converts an eight-byte long part from the specified byte array starting at the specified position, assuming it is a big-endian value, to a long-int value.- Parameters:
bytes- a given byte array.pos- the position within the byte array where the eight byte long part starts.- Returns:
- a long int value from the given byte array at the specified positon.
-
byteToUintLittleEndian
public static long byteToUintLittleEndian(int pos, byte[] bytes) Converts an four-byte long part from the specified byte array starting at the specified position, assuming it is a little-endian value, to an unsigned integer value.- Parameters:
bytes- a given byte array.pos- the position within the byte array where the four byte long part starts.- Returns:
- an unsigned integer value from the given byte array at the specified positon.
-
byteToIntBigEndian
public static int byteToIntBigEndian(int pos, byte[] bytes) Converts an four-byte long part from the specified byte array starting at the specified position, assuming it is a big-endian value, to an integer value.- Parameters:
bytes- a given byte array.pos- the position within the byte array where the four byte long part starts.- Returns:
- an integer value from the given byte array at the specified positon.
-
byteToUintBigEndian
public static long byteToUintBigEndian(int pos, byte[] bytes) Converts an four-byte long part from the specified byte array starting at the specified position, assuming it is a big-endian value, to an unsigned integer value.- Parameters:
bytes- a given byte array.pos- the position within the byte array where the four byte long part starts.- Returns:
- an unsigned integer value from the given byte array at the specified positon.
-
byteToWordLittleEndian
public static int byteToWordLittleEndian(int pos, byte[] bytes) Converts an two-byte long part from the specified byte array starting at the specified position, assuming it is a little-endian value, to an integer value.- Parameters:
bytes- a given byte array.pos- the position within the byte array where the two byte long part starts.- Returns:
- an integer value from the given byte array at the specified positon.
-
byteToWordBigEndian
public static int byteToWordBigEndian(int pos, byte[] bytes) Converts an two-byte long part from the specified byte array starting at the specified position, assuming it is a big-endian value, to an integer value.- Parameters:
bytes- a given byte array.pos- the position within the byte array where the two byte long part starts.- Returns:
- an integer value from the given byte array at the specified positon.
-
byteToShortIntBigEndian
public static short byteToShortIntBigEndian(int pos, byte[] bytes) Converts an two-byte long part from the specified byte array starting at the specified position, assuming it is a big-endian value, to a short integer value.- Parameters:
bytes- a given byte array.pos- the position within the byte array where the two byte long part starts.- Returns:
- a short integer value from the given byte array at the specified positon.
-
byteToShortIntLittleEndian
public static short byteToShortIntLittleEndian(int pos, byte[] bytes) Converts an two-byte long part from the specified byte array starting at the specified position, assuming it is a little-endian value, to a short int value.- Parameters:
bytes- a given byte array.pos- the position within the byte array where the two byte long part starts.- Returns:
- a short integer value from the given byte array at the specified positon.
-
byteToString
Creates a string using the specified part of the given byte array. The part is specified by a starting index and the number of byte to be used.- Parameters:
data- a given byte array.start- a starting index.length- the number of byte to be used.- Returns:
- a string built from a part of the byte array.
-
byteArrayToString
Generates a string consisting of the string representations of the hexadecimal values of the given ByteArray separated by spaces.- Parameters:
data- a given byte array.- Returns:
- a string with the hex values of the given byte array.
-
byteArrayToString
Generates a string consisting of the string representations of the hexadecimal values of the given ByteArray separated by spaces. If the block size "n" is greater than "0", hyphens are inserted between blocks with "n" hex numbers.- Parameters:
data- a given byte array.blockSize- number of hex values per block.- Returns:
- a string with the hex values of the given byte array.
-
insertIntAt
public static void insertIntAt(int v, int pos, byte[] data) Assigns the four bytes of an integer value to the bytes of the given byte array starting at the specified position with byte order big endian.- Parameters:
v- a given integer value.pos- the starting position in the byte array.data- a given byte array.
-
insertIntAt
public static void insertIntAt(long v, int pos, byte[] data) Deprecated.Use insertIntBigEndianAt instead.Assigns the four bytes of a long value to the bytes of the given byte array starting at the specified position with byte order big endian.- Parameters:
v- a given long value.pos- the starting position in the byte array.data- a given byte array.
-
insertIntBigEndianAt
public static void insertIntBigEndianAt(long v, int pos, byte[] data) Assigns the four lsb bytes of a long value to the bytes of the given byte array starting at the specified position with byte order big endian.- Parameters:
v- a given long value.pos- the starting position in the byte array.data- a given byte array.
-
insertIntLittleEndianAt
public static void insertIntLittleEndianAt(long v, int pos, byte[] data) Assigns the four lsb bytes of a long value to the bytes of the given byte array starting at the specified position with byte order little endian.- Parameters:
v- a given long value.pos- the starting position in the byte array.data- a given byte array.
-
insertLongLittleEndianAt
public static void insertLongLittleEndianAt(long v, int pos, byte[] data) Assigns the eight bytes of a long value to the bytes of the given byte array starting at the specified position with byte order little endian.- Parameters:
v- a given long value.pos- the starting position in the byte array.data- a given byte array.
-
insertShortIntAt
public static void insertShortIntAt(int v, int pos, byte[] data) Deprecated.Use insertShortIntBigEndianAt instead.Assigns the two lsb bytes of an int value to the bytes of the given byte array starting at the specified position with byte order big endian.- Parameters:
v- a given integer value.pos- the starting position in the byte array.data- a given byte array.
-
insertShortIntBigEndianAt
public static void insertShortIntBigEndianAt(int v, int pos, byte[] data) Assigns the two lsb bytes of an int value to the bytes of the given byte array starting at the specified position with byte order big endian.- Parameters:
v- a given integer value.pos- the starting position in the byte array.data- a given byte array.
-
insertShortIntLittleEndianAt
public static void insertShortIntLittleEndianAt(int v, int pos, byte[] data) Assigns the two lsb bytes of an int value to the bytes of the given byte array starting at the specified position with byte order little endian.- Parameters:
v- a given integer value.pos- the starting position in the byte array.data- a given byte array.
-
insertFloatAt
public static void insertFloatAt(int pos, float fValue, byte[] data) Converts a the float four bytes representation into an integer and places the four bytes of the integer into the byte array from the given position on.- Parameters:
pos- is the position in the byte array where the float value has to be placed at.fValue- is the float value to be placed into the byte array.data- is the byte array the float value shall be written to.
-
getFloatAt
public static float getFloatAt(int pos, byte[] data) Converts the four bytes at the given starting position to a floating point value assuming the byte order as little endian.- Parameters:
pos- starting position in the byte array.data- a given byte array.- Returns:
- an integer value.
-
getIntAt
public static int getIntAt(int pos, byte[] data) Converts the four bytes at the given starting position to an integer value assuming as little endian.- Parameters:
pos- starting position in the byte array.data- a given byte array.- Returns:
- an integer value.
-
getLongAt
public static long getLongAt(int pos, byte[] data) Converts the eight bytes at the given starting position to a long value assuming as little endian.- Parameters:
pos- starting position in the byte array.data- a given byte array.- Returns:
- a long value.
-
getHexString
Converts the byte values of the byte array into hex strings and arranges them one after another separated by spaces.- Parameters:
data- a given byte array.- Returns:
- a string with hex string separated by spaces.
-
printBuffer
Converts the specified byte array to a hex string and outputs it over the specified print stream.- Parameters:
out- a given print stream.data- a given byte array.
-
printBuffer
public static void printBuffer(byte[] data) Converts the specified byte array to a hex string and outputs it over the System.out print stream.- Parameters:
data- a given byte array.
-
printlnBuffer
public static void printlnBuffer(byte[] data) Converts the specified byte array to a hex string and outputs it over the System.out print stream followed by a System.out.println().- Parameters:
data- a given byte array.
-
createFormattedHexList
Generates a string representing a formatted hex list with sixteen bytes per line and a preceding eight-character hexadecimal address. The start value for the addresses is assumed to be zero. The string inserted between the eighth and ninth hexadecimal value in each line is assumed to two spaces.- Parameters:
data- a given byte array.- Returns:
- a string representing a formatted hex list.
-
createFormattedHexList
Generates a string representing a formatted hex list with sixteen bytes per line and a preceding eight-character hexadecimal address. The start value for the addresses is assumed to be zero.- Parameters:
data- a given byte array.centerDiv- a string inserted between the eighth and ninth hexadecimal value in each line.- Returns:
- a string representing a formatted hex list.
-
createFormattedHexList
Generates a string representing a formatted hex list with sixteen bytes per line and a preceding eight-character hexadecimal address.- Parameters:
adr- a given start value for the preceding address.data- a given byte array.centerDiv- a string inserted between the eighth and ninth hexadecimal value in each line.- Returns:
- a string representing a formatted hex list.
-
swapBytes
public static byte[] swapBytes(byte[] data) Reverses the order of the bytes in a byte array.- Parameters:
data- a given byte array.- Returns:
- the byte array where the order of the bytes was reversed.
-