Byte.empty constructor
Byte.empty()
Creates an empty Byte array.
Example:
final empty = Byte.empty();
print(empty.length); // 0
print(empty.isEmpty); // true
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]);
Implementation
Byte.empty() : _bytes = <int>[];