fromReadable static method

Uint8List? fromReadable(
  1. String? readable, {
  2. Radix radix = Radix.hex,
})

covnert readable string to bytes array. byte splitted by space or comma, e.g. '0xaa ff 01 02 ,03 ,00 A0' default radix is hex,or specified radix

Implementation

static Uint8List? fromReadable(String? readable, {Radix radix = Radix.hex}) {
  if (readable == null || readable.isEmpty) return null;

  final List<int> list = [];
  final split = RegExp(' +');
  final strArray = readable.replaceAll(',', ' ').split(split);

  for (String str in strArray) {
    var pure = str.trim();
    if (radix == Radix.hex && pure.startsWith('0x')) {
      pure = pure.substring(2);
    }

    final value = int.tryParse(pure, radix: radix == Radix.hex ? 16 : 10);
    if (value == null) return null;

    list.add(value);
  }

  if (list.length <= 0) return null;

  return Uint8List.fromList(list);
}