fromBuffer static method
Converts a binary buffer to a UUIDv4 string.
This method takes a binary buffer of 16 bytes and converts it into a UUIDv4 string in the format "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx". The UUIDv4 string is commonly used to represent unique identifiers.
Parameters:
buffer
: The binary buffer (List
Returns: A UUIDv4 string.
Example:
final buffer = List<int>.from([85, 14, 132, 0, 226, 155, 65, 212, 167, 22, 68, 102, 85, 68, 0, 0]);
final uuid = fromBuffer(buffer);
print(uuid); /// Output: '550e8400-e29b-41d4-a716-446655440000'
Throws:
- Exception if the input buffer's length is not 16 bytes, as UUIDv4 buffers must be exactly 16 bytes long.
Note: This method assumes that the input buffer contains valid UUIDv4 data.
Implementation
static String fromBuffer(List<int> buffer) {
if (buffer.length != 16) {
throw ArgumentException(
'Invalid buffer length. UUIDv4 buffers must be 16 bytes long.');
}
final List<String> hexBytes =
buffer.map((byte) => byte.toRadixString(16).padLeft(2, '0')).toList();
/// Insert dashes at appropriate positions to form a UUIDv4 string
return '${hexBytes.sublist(0, 4).join('')}-${hexBytes.sublist(4, 6).join('')}-${hexBytes.sublist(6, 8).join('')}-${hexBytes.sublist(8, 10).join('')}-${hexBytes.sublist(10).join('')}';
}