Java XTablesByteUtils

XTablesByteUtils is a Java utility class that enables conversions between primitive data types and byte arrays, adhering to big-endian binary format.

XTablesByteUtils Class Documentation

Overview

XTablesByteUtils is a utility class designed to facilitate the conversion between various data types (integers, longs, booleans, doubles, strings, etc.) and their corresponding byte array representations. The class ensures proper handling of byte arrays, performing conversions with length validation for proper data handling.

Key Features

  • Converts between byte arrays and primitive data types (integer, long, double, boolean, etc.).

  • Ensures proper length checks for byte arrays during conversion.

  • Supports both conversions to and from various data types.

Importing

To use XTablesByteUtils, simply import it into your project as follows:

import org.kobe.xbot.Utilities.XTablesByteUtils;

Methods

toInt(byte[] bytes)

Converts a byte array into an integer.

  • Parameters:

    • bytes: A byte array of length 4 to be converted to an integer.

  • Returns:

    • int: The integer value represented by the byte array.

  • Throws:

    • IllegalArgumentException: If the byte array is null or has a length other than 4.

toLong(byte[] bytes)

Converts a byte array into a long.

  • Parameters:

    • bytes: A byte array of length 8 to be converted to a long.

  • Returns:

    • long: The long value represented by the byte array.

  • Throws:

    • IllegalArgumentException: If the byte array is null or has a length other than 8.

toDouble(byte[] bytes)

Converts a byte array into a double.

  • Parameters:

    • bytes: A byte array of length 8 to be converted to a double.

  • Returns:

    • double: The double value represented by the byte array.

  • Throws:

    • IllegalArgumentException: If the byte array is null or has a length other than 8.

toString(byte[] bytes)

Converts a byte array into a string using UTF-8 decoding.

  • Parameters:

    • bytes: A byte array to be converted to a string.

  • Returns:

    • String: The string represented by the byte array.

  • Throws:

    • IllegalArgumentException: If the byte array is null.

toBoolean(byte[] bytes)

Converts a byte array into a boolean.

  • Parameters:

    • bytes: A byte array of length 1 to be converted to a boolean.

  • Returns:

    • boolean: The boolean value represented by the byte array (true if the byte is 0x01, false otherwise).

  • Throws:

    • IllegalArgumentException: If the byte array is null or has a length other than 1.

fromInteger(int i)

Converts an integer to a byte array.

  • Parameters:

    • i: The integer to convert.

  • Returns:

    • byte[]: The byte array representation of the integer.

fromLong(long i)

Converts a long to a byte array.

  • Parameters:

    • i: The long to convert.

  • Returns:

    • byte[]: The byte array representation of the long.

fromDouble(double i)

Converts a double to a byte array.

  • Parameters:

    • i: The double to convert.

  • Returns:

    • byte[]: The byte array representation of the double.

fromBoolean(boolean i)

Converts a boolean to a byte array.

  • Parameters:

    • i: The boolean to convert.

  • Returns:

    • byte[]: The byte array representation of the boolean (0x01 for true, 0x00 for false).

fromList(List<T> i)

Converts a list to a byte array.

  • Parameters:

    • i: The list to convert.

  • Returns:

    • byte[]: The byte array representation of the list (converted to its string form).

fromString(String i)

Converts a string to a byte array using UTF-8 encoding.

  • Parameters:

    • i: The string to convert.

  • Returns:

    • byte[]: The byte array representation of the string.

Usage Example

// Convert an integer to a byte array
byte[] intBytes = XTablesByteUtils.fromInteger(1234);
System.out.println(XTablesByteUtils.toInt(intBytes));  // Output: 1234

// Convert a string to a byte array
byte[] stringBytes = XTablesByteUtils.fromString("Hello, World!");
System.out.println(XTablesByteUtils.toString(stringBytes));  // Output: "Hello, World!"

// Convert a boolean to a byte array
byte[] boolBytes = XTablesByteUtils.fromBoolean(true);
System.out.println(XTablesByteUtils.toBoolean(boolBytes));  // Output: true

Notes

  • The class ensures proper validation of input byte arrays before performing conversions, throwing IllegalArgumentException if the byte array does not meet the expected length.

  • All methods for converting to and from byte arrays use big-endian format for binary data.

Last updated