Class XTablesByteUtils
This class provides static methods for converting byte arrays and ByteString objects into common Java types such as int, long, double, String, and boolean. These conversions are useful for handling raw byte data in a protocol-agnostic manner.
Author: Kobe Lei Version: 1.0 Package: org.kobe.xbot.Utilities
This class is a part of the XTABLES project and provides essential utility functions for byte conversion in data processing.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Map.Entry<XTableProto.XTableMessage.Type,
byte[]> static String
convertTypeValueToJsonString
(XTableProto.XTableMessage.Type type, byte[] value) static String
static String
coordinateListToString
(byte[] rawData) Converts a raw protobuf byte array into a human-readable string of coordinates.static String
coordinateListToString
(XTableValues.CoordinateList coordinateList) Converts a list of Coordinate protobuf objects into a human-readable string.static byte[]
fromBoolean
(boolean i) Converts a boolean into a byte array.static byte[]
fromDouble
(double i) Converts a double into a byte array.static byte[]
fromInteger
(int i) Converts an integer into a byte array.static byte[]
fromLong
(long i) Converts a long into a byte array.static byte[]
fromObject
(Object v) Converts an object into a byte array.static byte[]
fromString
(String i) Converts a string into a byte array.static XTableValues.Coordinate
getCoordinate
(int x, int y) static List<XTableValues.Coordinate>
getCoordinateList
(XTableValues.Coordinate... coordinate) static XTableValues.CoordinateList
getCoordinateListProto
(XTableValues.Coordinate... coordinate) static byte[]
packPose2d
(edu.wpi.first.math.geometry.Pose2d pose) Packs a Pose2d object into a byte array.static byte[]
packPose3d
(edu.wpi.first.math.geometry.Pose3d pose) Packs a Pose3d object into a byte array.static String
pose2dToString
(byte[] data) Converts a Pose2d byte array into a human-readable string.static String
pose2dToString
(edu.wpi.first.math.geometry.Pose2d pose) Converts a Pose2d byte array into a human-readable string.static String
pose3dToString
(byte[] data) Converts a Pose3d byte array into a human-readable string.static String
pose3dToString
(edu.wpi.first.math.geometry.Pose3d pose) Converts a Pose3d object into a human-readable string.static boolean
to_Primitive_Boolean
(byte[] bytes) Converts a byte array into a boolean.static double
to_Primitive_Double
(byte[] bytes) Converts a byte array into a double.static int
to_Primitive_Int
(byte[] bytes) Converts a byte array into an integer.static int
to_Primitive_Integer
(com.google.protobuf.ByteString byteString) Converts a ByteString into an integer.static long
to_Primitive_Long
(com.google.protobuf.ByteString byteString) Converts a ByteString into a long.static Boolean
toBoolean
(com.google.protobuf.ByteString byteString) Converts a ByteString into a boolean.static Double
toDouble
(com.google.protobuf.ByteString byteString) Converts a ByteString into a double.static Integer
toInteger
(byte[] byteString) static Integer
toInteger
(com.google.protobuf.ByteString byteString) static long
toLong
(byte[] bytes) Converts a byte array into a long.static Long
toLong
(com.google.protobuf.ByteString byteString) static <T> T
Converts a byte array back into an object of the specified class type.static String
toString
(byte[] bytes) Converts a byte array into a string.static String
toString
(com.google.protobuf.ByteString byteString) Converts a ByteString into a string.unpack_boolean_list
(com.google.protobuf.ByteString bytes) static List<com.google.protobuf.ByteString>
unpack_bytes_list
(com.google.protobuf.ByteString bytes) static List<XTableValues.Coordinate>
unpack_coordinates_list
(byte[] bytes) Converts a byte array into a list of Coordinate objects.static List<XTableValues.Coordinate>
unpack_coordinates_list
(com.google.protobuf.ByteString bytes) Converts a byte array into a list of Coordinate objects.unpack_double_list
(com.google.protobuf.ByteString bytes) unpack_float_list
(com.google.protobuf.ByteString bytes) unpack_integer_list
(com.google.protobuf.ByteString bytes) unpack_long_list
(com.google.protobuf.ByteString bytes) unpack_string_list
(com.google.protobuf.ByteString bytes) static edu.wpi.first.math.geometry.Pose2d
unpackPose2d
(byte[] data) Unpacks a byte array into a Pose2d object.static edu.wpi.first.math.geometry.Pose3d
unpackPose3d
(byte[] data) Unpacks a byte array into a Pose3d object.
-
Constructor Details
-
XTablesByteUtils
public XTablesByteUtils()
-
-
Method Details
-
convertXTableUpdateToJsonString
-
convertJsonStringToTypeValue
public static Map.Entry<XTableProto.XTableMessage.Type,byte[]> convertJsonStringToTypeValue(String json) -
getCoordinate
-
getCoordinateList
public static List<XTableValues.Coordinate> getCoordinateList(XTableValues.Coordinate... coordinate) -
getCoordinateListProto
public static XTableValues.CoordinateList getCoordinateListProto(XTableValues.Coordinate... coordinate) -
coordinateListToString
Converts a list of Coordinate protobuf objects into a human-readable string.- Parameters:
coordinateList
- The CoordinateList protobuf object.- Returns:
- A formatted string representing the list of coordinates.
-
unpack_coordinates_list
Converts a byte array into a list of Coordinate objects.- Parameters:
bytes
- The byte array representing a serialized CoordinateList.- Returns:
- A list of Coordinate objects, or null if parsing fails.
-
unpack_string_list
-
unpack_double_list
-
unpack_integer_list
-
unpack_long_list
-
unpack_float_list
-
unpack_boolean_list
-
unpack_bytes_list
public static List<com.google.protobuf.ByteString> unpack_bytes_list(com.google.protobuf.ByteString bytes) -
unpack_coordinates_list
public static List<XTableValues.Coordinate> unpack_coordinates_list(com.google.protobuf.ByteString bytes) Converts a byte array into a list of Coordinate objects.- Parameters:
bytes
- The byte array representing a serialized CoordinateList.- Returns:
- A list of Coordinate objects, or null if parsing fails.
-
coordinateListToString
Converts a raw protobuf byte array into a human-readable string of coordinates.- Parameters:
rawData
- The byte array representing a serialized CoordinateList.- Returns:
- A formatted string representation of the list of coordinates, or an error message if parsing fails.
-
convertTypeValueToJsonString
public static String convertTypeValueToJsonString(XTableProto.XTableMessage.Type type, byte[] value) -
to_Primitive_Int
public static int to_Primitive_Int(byte[] bytes) Converts a byte array into an integer.This method interprets the first four bytes of the byte array as an integer. It expects the byte array to have a length of 4.
- Parameters:
bytes
- The byte array to convert.- Returns:
- The integer value represented by the byte array.
- Throws:
IllegalArgumentException
- If the byte array is null or does not have a length of 4.
-
toLong
public static long toLong(byte[] bytes) Converts a byte array into a long.This method interprets the first eight bytes of the byte array as a long. It expects the byte array to have a length of 8.
- Parameters:
bytes
- The byte array to convert.- Returns:
- The long value represented by the byte array.
- Throws:
IllegalArgumentException
- If the byte array is null or does not have a length of 8.
-
to_Primitive_Double
public static double to_Primitive_Double(byte[] bytes) Converts a byte array into a double.This method interprets the first eight bytes of the byte array as a double. It expects the byte array to have a length of 8.
- Parameters:
bytes
- The byte array to convert.- Returns:
- The double value represented by the byte array.
- Throws:
IllegalArgumentException
- If the byte array is null or does not have a length of 8.
-
toString
Converts a byte array into a string.This method decodes the byte array into a string using UTF-8 encoding.
- Parameters:
bytes
- The byte array to convert.- Returns:
- The string represented by the byte array.
- Throws:
IllegalArgumentException
- If the byte array is null.
-
to_Primitive_Boolean
public static boolean to_Primitive_Boolean(byte[] bytes) Converts a byte array into a boolean.This method checks if the first byte of the byte array is equal to 0x01, which represents true in boolean form.
- Parameters:
bytes
- The byte array to convert.- Returns:
- The boolean value represented by the byte array.
- Throws:
IllegalArgumentException
- If the byte array is null or does not have a length of 1.
-
to_Primitive_Integer
public static int to_Primitive_Integer(com.google.protobuf.ByteString byteString) Converts a ByteString into an integer.This method interprets the first four bytes of the ByteString as an integer. It expects the ByteString to have a size of 4.
- Parameters:
byteString
- The ByteString to convert.- Returns:
- The integer value represented by the ByteString.
- Throws:
IllegalArgumentException
- If the ByteString is null or does not have a size of 4.
-
toInteger
-
toInteger
-
to_Primitive_Long
public static long to_Primitive_Long(com.google.protobuf.ByteString byteString) Converts a ByteString into a long.This method interprets the first eight bytes of the ByteString as a long. It expects the ByteString to have a size of 8.
- Parameters:
byteString
- The ByteString to convert.- Returns:
- The long value represented by the ByteString.
- Throws:
IllegalArgumentException
- If the ByteString is null or does not have a size of 8.
-
toLong
-
toDouble
Converts a ByteString into a double.This method interprets the first eight bytes of the ByteString as a double. It expects the ByteString to have a size of 8.
- Parameters:
byteString
- The ByteString to convert.- Returns:
- The double value represented by the ByteString.
- Throws:
IllegalArgumentException
- If the ByteString is null or does not have a size of 8.
-
toString
Converts a ByteString into a string.This method decodes the ByteString into a string using UTF-8 encoding.
- Parameters:
byteString
- The ByteString to convert.- Returns:
- The string represented by the ByteString.
- Throws:
IllegalArgumentException
- If the ByteString is null.
-
toBoolean
Converts a ByteString into a boolean.This method checks if the first byte of the ByteString is equal to 0x01, which represents true in boolean form.
- Parameters:
byteString
- The ByteString to convert.- Returns:
- The boolean value represented by the ByteString.
- Throws:
IllegalArgumentException
- If the ByteString is null or does not have a size of 1.
-
fromInteger
public static byte[] fromInteger(int i) Converts an integer into a byte array.This method converts the provided integer into a 4-byte array.
- Parameters:
i
- The integer to convert.- Returns:
- The byte array representing the integer.
-
fromLong
public static byte[] fromLong(long i) Converts a long into a byte array.This method converts the provided long into an 8-byte array.
- Parameters:
i
- The long to convert.- Returns:
- The byte array representing the long.
-
fromDouble
public static byte[] fromDouble(double i) Converts a double into a byte array.This method converts the provided double into an 8-byte array.
- Parameters:
i
- The double to convert.- Returns:
- The byte array representing the double.
-
fromBoolean
public static byte[] fromBoolean(boolean i) Converts a boolean into a byte array.This method converts the provided boolean into a byte array. True is represented by 0x01, and false by 0x00.
- Parameters:
i
- The boolean to convert.- Returns:
- The byte array representing the boolean.
-
fromString
Converts a string into a byte array.This method converts the provided string into a byte array using UTF-8 encoding.
- Parameters:
i
- The string to convert.- Returns:
- The byte array representing the string.
-
fromObject
Converts an object into a byte array.This method serializes the provided object into a JSON string using Gson, then converts the JSON string into a byte array using UTF-8 encoding.
- Parameters:
v
- The object to convert.- Returns:
- A byte array representing the object in JSON format.
-
toObject
Converts a byte array back into an object of the specified class type.This method deserializes the byte array into a JSON string, then uses Gson to convert the JSON string into an object of the specified type.
- Type Parameters:
T
- The type of the object.- Parameters:
v
- The byte array to convert.clazz
- The class type of the object to return.- Returns:
- The deserialized object of type T.
- Throws:
XTablesException
- If there is an error during deserialization.
-
packPose3d
public static byte[] packPose3d(edu.wpi.first.math.geometry.Pose3d pose) Packs a Pose3d object into a byte array.- Parameters:
pose
- The Pose3d object to pack.- Returns:
- A byte array representation of the Pose3d object.
-
pose3dToString
Converts a Pose3d byte array into a human-readable string. If deserialization fails, it returns "Invalid Pose3d Data".- Parameters:
data
- The byte array representing a serialized Pose3d.- Returns:
- A human-readable string representation of the Pose3d.
-
pose3dToString
Converts a Pose3d object into a human-readable string. If deserialization fails, it returns "Invalid Pose3d Data".- Parameters:
pose
- The Pose3d object.- Returns:
- A human-readable string representation of the Pose3d.
-
unpackPose3d
public static edu.wpi.first.math.geometry.Pose3d unpackPose3d(byte[] data) Unpacks a byte array into a Pose3d object.- Parameters:
data
- The byte array to unpack.- Returns:
- A Pose3d object if successful, or null if unpacking fails.
-
packPose2d
public static byte[] packPose2d(edu.wpi.first.math.geometry.Pose2d pose) Packs a Pose2d object into a byte array.- Parameters:
pose
- The Pose2d object to pack.- Returns:
- A byte array representation of the Pose2d object.
-
pose2dToString
Converts a Pose2d byte array into a human-readable string. If deserialization fails, it returns "Invalid Pose2d Data".- Parameters:
data
- The byte array representing a serialized Pose2d.- Returns:
- A human-readable string representation of the Pose2d.
-
pose2dToString
Converts a Pose2d byte array into a human-readable string. If deserialization fails, it returns "Invalid Pose2d Data".- Parameters:
pose
- The pose- Returns:
- A human-readable string representation of the Pose2d.
-
unpackPose2d
public static edu.wpi.first.math.geometry.Pose2d unpackPose2d(byte[] data) Unpacks a byte array into a Pose2d object.- Parameters:
data
- The byte array to unpack.- Returns:
- A Pose2d object if successful, or null if unpacking fails.
-