Package org.uncommons.maths.binary
Class BinaryUtils
- java.lang.Object
-
- org.uncommons.maths.binary.BinaryUtils
-
public final class BinaryUtils extends Object
Utility methods for working with binary and hex data.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
convertBytesToHexString(byte[] data)
Converts an array of bytes in to a String of hexadecimal characters (0 - F).static int
convertBytesToInt(byte[] bytes, int offset)
Take four bytes from the specified position in the specified block and convert them into a 32-bit int, using the big-endian convention.static int[]
convertBytesToInts(byte[] bytes)
Convert an array of bytes into an array of ints.static long
convertBytesToLong(byte[] bytes, int offset)
Utility method to convert an array of bytes into a long.static BitString
convertDoubleToFixedPointBits(double value)
Converts a floating point value in the range 0 - 1 into a fixed point bit string (where the most significant bit has a value of 0.5).static byte[]
convertHexStringToBytes(String hex)
Converts a hexadecimal String (such as one generated by theconvertBytesToHexString(byte[])
method) into an array of bytes.
-
-
-
Method Detail
-
convertBytesToHexString
public static String convertBytesToHexString(byte[] data)
Converts an array of bytes in to a String of hexadecimal characters (0 - F).- Parameters:
data
- An array of bytes to convert to a String.- Returns:
- A hexadecimal String representation of the data.
-
convertHexStringToBytes
public static byte[] convertHexStringToBytes(String hex)
Converts a hexadecimal String (such as one generated by theconvertBytesToHexString(byte[])
method) into an array of bytes.- Parameters:
hex
- The hexadecimal String to be converted into an array of bytes.- Returns:
- An array of bytes that.
-
convertBytesToInt
public static int convertBytesToInt(byte[] bytes, int offset)
Take four bytes from the specified position in the specified block and convert them into a 32-bit int, using the big-endian convention.- Parameters:
bytes
- The data to read from.offset
- The position to start reading the 4-byte int from.- Returns:
- The 32-bit integer represented by the four bytes.
-
convertBytesToInts
public static int[] convertBytesToInts(byte[] bytes)
Convert an array of bytes into an array of ints. 4 bytes from the input data map to a single int in the output data.- Parameters:
bytes
- The data to read from.- Returns:
- An array of 32-bit integers constructed from the data.
- Since:
- 1.1
-
convertBytesToLong
public static long convertBytesToLong(byte[] bytes, int offset)
Utility method to convert an array of bytes into a long. Byte ordered is assumed to be big-endian.- Parameters:
bytes
- The data to read from.offset
- The position to start reading the 8-byte long from.- Returns:
- The 64-bit integer represented by the eight bytes.
- Since:
- 1.1
-
convertDoubleToFixedPointBits
public static BitString convertDoubleToFixedPointBits(double value)
Converts a floating point value in the range 0 - 1 into a fixed point bit string (where the most significant bit has a value of 0.5).- Parameters:
value
- The value to convert (must be between zero and one).- Returns:
- A bit string representing the value in fixed-point format.
-
-