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<int>
) representing the UUID.
Implementation
static List<int> toBuffer(String uuidString, {bool validate = true}) {
if (validate && !isValidUUIDv4(uuidString)) {
throw ArgumentException("invalid uuid string.",
details: {"uuid": uuidString});
}
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;
}