Byte class

A comprehensive wrapper class for byte operations, supporting both single bytes and byte collections.

This class provides Java-like functionality for working with 8-bit signed integers (-128 to 127) and collections of bytes. It can represent a single byte value or work with byte arrays, similar to Java's Byte class and byte[] arrays.

Key Features:

  • Single byte operations: Wrap and manipulate individual byte values
  • Byte array support: Work with collections of bytes (List<int>)
  • Type conversions: Convert between signed/unsigned, different radixes
  • Bitwise operations: Full support for bit manipulation
  • Java compatibility: Method signatures and behavior match Java's Byte class

Usage Examples:

Single Byte Operations:

// Create single bytes
final b1 = Byte(127);                    // Maximum positive value
final b2 = Byte(-128);                   // Minimum negative value
final b3 = Byte.parseByte('42');         // Parse from string
final b4 = Byte.parseByte('FF', 16);     // Parse hex

// Basic operations
print(b1.value);                        // 127
print(b1.toUnsigned());                 // 127
print(b2.toUnsigned());                 // 128 (unsigned representation)
print(b1 + b2);                         // Byte(-1)

// Bitwise operations
final result = b1 & Byte(15);           // Bitwise AND
print(result.toBinaryString());         // Binary representation

Byte Array Operations:

// Create from byte arrays
final bytes = Byte.fromList([65, 66, 67]);        // From List<int>
final fromString = Byte.fromString('Hello');       // From string
final fromHex = Byte.fromHexString('48656C6C6F');  // From hex string

// Array operations
print(bytes.toList());                   // [65, 66, 67]
print(bytes.toString());                 // 'ABC'
print(bytes.toHexString());              // '414243'

// Manipulate arrays
bytes.append(68);                        // Add byte
bytes.appendAll([69, 70]);               // Add multiple bytes
final subBytes = bytes.subBytes(1, 3);   // Get substring

Advanced Operations:

// Validation and ranges
print(Byte.isValidByte(200));            // false (out of range)
print(Byte.isValidByte(-100));           // true

// Conversions
final unsigned = Byte.toUnsignedByte(200);  // Convert unsigned to signed
final signed = Byte.toSignedByte(200);      // Handle overflow

// Utility operations
final checksum = Byte.calculateChecksum([1, 2, 3, 4]);
final reversed = Byte.reverseBytes([1, 2, 3, 4]);
Implemented types

Constructors

Byte(int value)
Creates a Byte representing a single byte value.
Byte.empty()
Creates an empty Byte array.
Byte.fromHexString(String hexString)
Creates a Byte from a hexadecimal string.
Byte.fromList(List<int> bytes)
Creates a Byte from a list of byte values.
Byte.fromString(String str)
Creates a Byte from a string, converting each character to its byte value.
Byte.fromUint8List(Uint8List data)
Creates a Byte from a Uint8List.
Byte.fromUnsignedList(List<int> bytes)
Creates a Byte from an unsigned byte list (0-255 range).

Properties

hashCode int
Returns the hash code for this Byte.
no setteroverride
isEmpty bool
Returns true if this Byte object contains no bytes.
no setter
isNotEmpty bool
Returns true if this Byte object contains at least one byte.
no setter
isSingleByte bool
Returns true if this represents a single byte value.
no setter
length int
Returns the number of bytes in this Byte object.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
value int
Returns the byte value if this represents a single byte.
no setter

Methods

abs() Byte
Returns the absolute value of this Byte.
append(int byte) → void
Appends a single byte to this byte array.
appendAll(List<int> bytes) → void
Appends multiple bytes to this byte array.
clear() → void
Removes all bytes from this byte array.
compareTo(Byte other) int
Compares this Byte with another Byte.
override
insert(int index, int byte) → void
Inserts a byte at the specified index.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
removeAt(int index) int
Removes the byte at the specified index.
subBytes(int start, [int? end]) Byte
Returns a sub-array of bytes from start to end (exclusive).
toBinaryString() String
Returns a binary string representation of the bytes.
toHexString() String
Returns a hexadecimal string representation of the bytes.
toList() List<int>
Returns the bytes as a List<int> with signed values (-128 to 127).
toRadixString(int radix) String
Returns a string representation in the specified radix.
toString() String
Converts bytes to a string by interpreting each byte as a character code.
override
toUint8List() Uint8List
Returns the bytes as a Uint8List.
toUnsigned() int
Returns the unsigned representation of this byte (0-255).
toUnsignedList() List<int>
Returns the bytes as a List<int> with unsigned values (0 to 255).

Operators

operator %(Byte other) Byte
Modulo operation of two Byte values.
operator &(Byte other) Byte
Bitwise AND operation.
operator *(Byte other) Byte
Multiplies two Byte values.
operator +(Byte other) Byte
Adds two Byte values.
operator -(Byte other) Byte
Subtracts two Byte values.
operator <(Byte other) bool
Less than operator.
operator <<(int shiftAmount) Byte
Left shift operation.
operator <=(Byte other) bool
Less than or equal operator.
operator ==(Object other) bool
Returns true if this Byte equals the specified object.
override
operator >(Byte other) bool
Greater than operator.
operator >=(Byte other) bool
Greater than or equal operator.
operator >>(int shiftAmount) Byte
Right shift operation.
operator [](int index) int
Returns the byte at the specified index.
operator []=(int index, int value) → void
Sets the byte at the specified index.
operator ^(Byte other) Byte
Bitwise XOR operation.
operator unary-() Byte
Unary minus operator.
operator |(Byte other) Byte
Bitwise OR operation.
operator ~() Byte
Bitwise NOT operation.
operator ~/(Byte other) Byte
Integer division of two Byte values.

Static Methods

calculateChecksum(List<int> bytes) int
Calculates a simple checksum of a byte array.
isValidByte(int value) bool
Checks if a value is within the valid signed byte range (-128 to 127).
isValidUnsignedByte(int value) bool
Checks if a value is within the valid unsigned byte range (0 to 255).
parseByte(String str, [int radix = 10]) Byte
Parses a string to a Byte representing a single byte value.
reverseBytes(List<int> bytes) List<int>
Reverses the order of bytes in a list.
toSignedByte(int unsignedValue) int
Converts an unsigned byte value (0-255) to signed byte (-128-127).
toUnsignedByte(int signedValue) int
Converts a signed byte value (-128-127) to unsigned byte (0-255).
valueOf(int value) Byte
Returns a Byte instance representing the specified int value.

Constants

MAX_UNSIGNED_VALUE → const int
Maximum value for an unsigned byte (2^8 - 1)
MAX_VALUE → const int
Maximum value for a signed byte (2^7 - 1)
MIN_UNSIGNED_VALUE → const int
Minimum value for an unsigned byte
MIN_VALUE → const int
Minimum value for a signed byte (-2^7)