Python XTablesByteUtils

XTablesByteUtils is a Python utility class that facilitates conversions between various data types and their byte array representations, ensuring big-endian binary formatting.

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. It uses the struct module to perform binary packing and unpacking in big-endian format.

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

After installation, you can import the XTablesByteUtils as follows:

from JXTABLES import XTablesByteUtils

Methods

to_int(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.

Raises:

  • ValueError: If the byte array is None or has a length other than 4.


to_long(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.

Raises:

  • ValueError: If the byte array is None or has a length other than 8.


to_double(bytes)

Converts a byte array into a double.

Parameters:

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

Returns:

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

Raises:

  • ValueError: If the byte array is None or has a length other than 8.


to_string(bytes)

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

Parameters:

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

Returns:

  • str: The string represented by the byte array.

Raises:

  • ValueError: If the byte array is None.


to_boolean(bytes)

Converts a byte array into a boolean.

Parameters:

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

Returns:

  • bool: The boolean value represented by the byte array (True if the byte is 0x01, False otherwise).

Raises:

  • ValueError: If the byte array is None or has a length other than 1.


from_integer(i)

Converts an integer to a byte array.

Parameters:

  • i: The integer to convert.

Returns:

  • bytes: The byte array representation of the integer.


from_long(i)

Converts a long to a byte array.

Parameters:

  • i: The long to convert.

Returns:

  • bytes: The byte array representation of the long.


from_double(i)

Converts a double to a byte array.

Parameters:

  • i: The double to convert.

Returns:

  • bytes: The byte array representation of the double.


from_boolean(i)

Converts a boolean to a byte array.

Parameters:

  • i: The boolean to convert.

Returns:

  • bytes: The byte array representation of the boolean (0x01 for True, 0x00 for False).


from_list(i)

Converts a list to a byte array.

Parameters:

  • i: The list to convert.

Returns:

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


from_string(i)

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

Parameters:

  • i: The string to convert.

Returns:

  • bytes: The byte array representation of the string.


Usage Example

# Convert an integer to a byte array
int_bytes = XTablesByteUtils.from_integer(1234)
print(XTablesByteUtils.to_int(int_bytes))  # Output: 1234

# Convert a string to a byte array
string_bytes = XTablesByteUtils.from_string("Hello, World!")
print(XTablesByteUtils.to_string(string_bytes))  # Output: "Hello, World!"

# Convert a boolean to a byte array
bool_bytes = XTablesByteUtils.from_boolean(True)
print(XTablesByteUtils.to_boolean(bool_bytes))  # Output: True

Notes

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

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

Last updated