toBuffer static method
Converts a UUID string to a binary buffer.
This method takes a UUID string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", removes hyphens, and converts it to a binary buffer of 16 bytes. The binary buffer representation of a UUID can be useful for certain operations, such as storage or transmission.
Parameters:
uuidString
: The UUID string to convert to a binary buffer.
Returns: A binary buffer (List
Example:
final uuid = '550e8400-e29b-41d4-a716-446655440000';
final buffer = toBuffer(uuid);
print(buffer); /// Output: [85, 14, 132, 0, 226, 155, 65, 212, 167, 22, 68, 102, 85, 68, 0, 0]
Implementation
static List<int> toBuffer(String uuidString) {
if (!isValidUUIDv4(uuidString)) {
throw ArgumentException("invalid uuid string.");
}
final buffer = List<int>.filled(16, 0);
/// Remove dashes and convert the hexadecimal string to bytes
final cleanUuidString = uuidString.replaceAll('-', '');
final bytes = BytesUtils.fromHexString(cleanUuidString);
/// Copy the bytes into the buffer
for (var i = 0; i < 16; i++) {
buffer[i] = bytes[i];
}
return buffer;
}