首页 > 解决方案 > FormatException Uint8List 使用 dart 转换为字符串

问题描述

我正在尝试使用 Dart(在 Flutter 项目中)将 Uint8List 转换为字符串。我正在使用 Flutter Android USB 串行插件(https://github.com/altera2015/usbserial

数据来自 USB 设备,并作为流从库中返回。

如果输出为字符串,它看起来像:

[255,0,0,0,255....]

当我尝试:

String newTag = ascii.decode(asyncSnapshot.data);

我得到错误:

FormatException:Invalid value in input: 255

我不知道如何解决这个问题,我的结果应该是:

"352206000079439"

标签: flutterdart

解决方案


试试这个;

List<int> list = 'someData'.codeUnits;
Uint8List bytes = Uint8List.fromList(list);
String string = String.fromCharCodes(bytes);

推荐阅读