serializeValue function

ByteBuffer serializeValue(
  1. int major,
  2. int minor,
  3. String val
)

Implementation

ByteBuffer serializeValue(int major, int minor, String val) {
  // Remove everything that's not an hexadecimal character. These are not
  // considered errors since the value was already validated and they might
  // be number decimals or sign.
  String value = val.replaceAll('r[^0-9a-fA-F]', '');
  // Create the buffer from the value with left padding with 0.
  final length = math.pow(2, minor - 24).toInt();
  final temp = value.substring(
    value.length <= length * 2 ? 0 : value.length - length * 2,
  );
  final prefix = '0' * (2 * length - temp.length);
  value = prefix + temp;
  final bytes = [(major << 5) + minor];
  final arr = <int>[];
  for (int i = 0; i < value.length; i += 2) {
    arr.add(int.parse(value.substring(i, i + 2), radix: 16));
  }
  bytes.addAll(arr);
  return Uint8List.fromList(bytes).buffer;
}