Enum BinaryUtils
- java.lang.Object
-
- java.lang.Enum<BinaryUtils>
-
- io.github.pr0methean.betterrandom.util.BinaryUtils
-
- All Implemented Interfaces:
Serializable
,Comparable<BinaryUtils>
public enum BinaryUtils extends Enum<BinaryUtils>
Utility methods for working with binary and hex data.- Author:
- Daniel Dyer
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
convertBytesToHexString(byte[] data)
Converts an array of bytes into 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)
Convert a byte array to a long, reversingconvertLongToBytes(long)
.static long
convertBytesToLong(byte[] bytes, int offset)
Take eight bytes from the specified position in the specified block and convert them into a long, using the big-endian convention.static byte[]
convertHexStringToBytes(String hex)
Converts a hexadecimal String (such as one generated by theconvertBytesToHexString(byte[])
method) into an array of bytes.static byte[]
convertIntToBytes(int input)
Convert an int to an array of 4 bytes.static void
convertIntToBytes(int input, byte[] output, int offset)
Writes an int to 4 cells of a byte array.static byte[]
convertLongToBytes(long input)
Converts a long to an array of bytes.static void
convertLongToBytes(long input, byte[] output, int offset)
Unpacks a long into an existing byte array in big-endian order.static BinaryUtils
valueOf(String name)
Returns the enum constant of this type with the specified name.static BinaryUtils[]
values()
Returns an array containing the constants of this enum type, in the order they are declared.
-
-
-
Method Detail
-
values
public static BinaryUtils[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:for (BinaryUtils c : BinaryUtils.values()) System.out.println(c);
- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
public static BinaryUtils valueOf(String name)
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is null
-
convertBytesToHexString
public static String convertBytesToHexString(@Nullable byte[] data)
Converts an array of bytes into 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)
Take eight bytes from the specified position in the specified block and convert them into a long, using the big-endian convention.- Parameters:
bytes
- The data to read from.offset
- The position to start reading the long from.- Returns:
- The 32-bit integer represented by the eight bytes.
-
convertLongToBytes
public static byte[] convertLongToBytes(long input)
Converts a long to an array of bytes.The returned array will be reused on subsequent calls to this method from the same thread, so a defensive copy may be necessary.
- Parameters:
input
- a long.- Returns:
- an array of 8 bytes containing the long's value in big-endian order.
-
convertLongToBytes
public static void convertLongToBytes(long input, byte[] output, int offset)
Unpacks a long into an existing byte array in big-endian order.- Parameters:
input
- a long.output
- the arrayoffset
- the index to write the first (most significant) byte.
-
convertIntToBytes
public static void convertIntToBytes(int input, byte[] output, int offset)
Writes an int to 4 cells of a byte array.- Parameters:
input
- the int to writeoutput
- the array to write tooffset
- the first index to write to
-
convertIntToBytes
public static byte[] convertIntToBytes(int input)
Convert an int to an array of 4 bytes.The returned array will be reused on subsequent calls to this method from the same thread, so a defensive copy may be necessary.
- Parameters:
input
- an int.- Returns:
- an array of 4 bytes containing the int's value in big-endian order.
-
convertBytesToLong
public static long convertBytesToLong(byte[] bytes)
Convert a byte array to a long, reversingconvertLongToBytes(long)
.- Parameters:
bytes
- an 8-byte array inByteOrder.nativeOrder()
order.- Returns:
bytes
as a long.
-
-