fromReadable static method

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

将可读字符串转换为字节数组,用空格或逗号分隔的字节。 默认的基数是十六进制,或者指定的基数 比如:'01 02, ff 0x10,0xfa , 90 76 AF a0' 输出:1, 2, 255, 16, 250, 144, 118, 175, 160

Implementation

static Uint8List? fromReadable(String readable, {Radix radix = Radix.hex}) {
  if (TextUtils.isEmpty(readable)) {
    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.isEmpty) return null;

  return Uint8List.fromList(list);
}