Package com.github.tadukoo.util
Class ByteUtil
java.lang.Object
com.github.tadukoo.util.ByteUtil
Util functions for dealing with bytes.
- Version:
- Beta v.0.5
- Author:
- Logan Ferree (Tadukoo)
-
Field Summary
Modifier and TypeFieldDescriptionstatic final char[]
An array containing 0-9 and then A-F, used for converting to hex -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
checkBit
(byte bite, int position) Checks if the bit at the given position is set or notstatic byte
clearBit
(byte bite, int position) Clears the bit at the given position (sets to 0/false)static byte[]
Convert the given hex string into a byte arraystatic int
getBit
(byte bite, int position) Gets the bit at the given positionstatic int
hexToInt
(char hexChar) Converts the given hex character to an intstatic Byte
Parse the given String into a byte.static byte
setBit
(byte bite, int position) Sets the bit at the given position (to 1/true)static String
toBinaryString
(byte bite) Parse the given byte into a binary stringstatic byte
toggleBit
(byte bite, int position) Toggles the bit at the given positionstatic String
toHex
(byte bite) Converts the given byte to a 2 digit hex stringstatic String
toHex
(byte[] bytes) Converts the given byte array into a hex stringstatic int
toSignedInt
(byte bite) Returns the byte as an int, signed (-128 to 127)static int
toUnsignedInt
(byte bite) Returns the byte as an unsigned int (0 to 255)
-
Field Details
-
hexChars
public static final char[] hexCharsAn array containing 0-9 and then A-F, used for converting to hex
-
-
Constructor Details
-
ByteUtil
private ByteUtil()Not allowed to create a ByteUtil
-
-
Method Details
-
getBit
public static int getBit(byte bite, int position) Gets the bit at the given position- Parameters:
bite
- The byte to grab a bit fromposition
- The position of the bit to be grabbed- Returns:
- The value of the bit at the given position
-
setBit
public static byte setBit(byte bite, int position) Sets the bit at the given position (to 1/true)- Parameters:
bite
- The byte to set the bit onposition
- The position of the bit to be set- Returns:
- A byte with the given bit set
-
clearBit
public static byte clearBit(byte bite, int position) Clears the bit at the given position (sets to 0/false)- Parameters:
bite
- The byte to clear the bit onposition
- The position of the bit to be cleared- Returns:
- A byte with the given bit cleared
-
toggleBit
public static byte toggleBit(byte bite, int position) Toggles the bit at the given position- Parameters:
bite
- The byte to toggle the bit onposition
- The position of the bit to be toggled- Returns:
- A byte with the given bit toggled
-
checkBit
public static boolean checkBit(byte bite, int position) Checks if the bit at the given position is set or not- Parameters:
bite
- The byte to check a bit fromposition
- The position of the bit to be checked- Returns:
- true if the bit is set (equal to 1) or false if not set (equal to 0)
-
toBinaryString
Parse the given byte into a binary string- Parameters:
bite
- The byte to be converted to a binary string- Returns:
- The binary string representation of the byte
-
parseByte
Parse the given String into a byte. String should be a binary representation of a byte- Parameters:
byteString
- The binary representation of a byte- Returns:
- A Byte parsed from the given binary String
-
toSignedInt
public static int toSignedInt(byte bite) Returns the byte as an int, signed (-128 to 127)- Parameters:
bite
- The byte to convert to an int- Returns:
- The int value of the given byte
-
toUnsignedInt
public static int toUnsignedInt(byte bite) Returns the byte as an unsigned int (0 to 255)- Parameters:
bite
- The byte to convert to an int- Returns:
- The int value of the given byte
-
toHex
Converts the given byte to a 2 digit hex string- Parameters:
bite
- The byte to convert to hex- Returns:
- The hex representation of the given byte
-
toHex
Converts the given byte array into a hex string- Parameters:
bytes
- The bytes to convert to hex- Returns:
- The hex representation of the given bytes
-
hexToInt
public static int hexToInt(char hexChar) Converts the given hex character to an int- Parameters:
hexChar
- The hex character to convert- Returns:
- The int value of the hex character, or -1 if invalid
-
fromHex
Convert the given hex string into a byte array- Parameters:
hex
- The hex string to convert- Returns:
- A byte array representing the given hex string
-