dart - 如何在 Dart 中读取字节数组数据?
问题描述
连接 TCP Socket 服务器并发送请求。并且服务器也在字节数组中发送响应。如何在 dart 中读取字节数组数据。
Socket.connect('localhost', 8081)
.then((socket) {
//Establish the onData, and onDone callbacks
socket.listen((data) {
print(new String.fromCharCodes(data).trim()); //Here data is byte[]
//How to read byte array data
},
onDone: () {
print("Done");
// socket.destroy();
},
onError: (e) {
print('Server error: $e');
});
socket.add([255, 12, 0, 11, 0, 9, 34, 82, 69, 70, 84, 65, 72, 73, 76]);
});
}
解决方案
它取决于数据类型被编码为字节。假设它是String
那么你可以用dart:convert
图书馆来做。
import 'dart:convert' show utf8;
final decoded = utf8.decode(data);
推荐阅读
- hibernate - Spring Boot Hibernate方言属性的功能?
- python - 如何将我的 hmmlearn 算法与 MFCC 数组相匹配?(梅尔频率倒谱系数) hmmlearn.GaussianHMM.fit 只需要序列
- ibm-cloud - 当我在同一个短语上有两个或多个“相同”实体时如何获得第二个@entitie.literal
- javascript - 如何在 Angular 的每次导航中运行 JS 方法
- reactjs - 显示我从 React Native 中的 JSON 文件访问的本地图像时出现问题
- c# - 如何传递 HttpInputStream 的内容并将其发送到 c# .net 中的另一个 Rest Api
- go - 带有许多嵌套的结构填充错误
- neo4j - 可选匹配上的 Cyper 合并
- javascript - 显示不正确
- perl - 如何将字母替换为某个数字