encoding - 如何将 Dart 的ByteData转换为字符串?

我正在读取一个二进制文件,并希望将其转换为字符串。如何在Dart中完成?

最佳答案

尝试以下

String getStringFromBytes(ByteData data) {final buffer = data.buffer;var list = buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);return utf8.decode(list);
}

ByteData 是一个抽象:

一个固定长度的随机访问字节序列,它还提供对这些字节表示的固定宽度整数和浮点数的随机和未对齐访问。

正如 Gunter 在评论中提到的,您可以使用File.writeAsBytes. 但是,它确实需要一些 API 工作才能从ByteDataList<int>

import 'dart:async';
import 'dart:io';
import 'dart:typed_data';Future<void> writeToFile(ByteData data, String path) {final buffer = data.buffer;return new File(path).writeAsBytes(buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));

你需要安装path_provider包,然后

这应该工作:

import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:path_provider/path_provider.dart';final dbBytes = await rootBundle.load('assets/file'); // <= your ByteData//=======================
Future<File> writeToFile(ByteData data) async {final buffer = data.buffer;Directory tempDir = await getTemporaryDirectory();String tempPath = tempDir.path;var filePath = tempPath + '/file_01.tmp'; // file_01.tmp is dump file, can be anythingreturn new File(filePath).writeAsBytes(buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
}
//======================

获取您的文件:

var file;
try {file = await writeToFile(dbBytes); // <= returns File
} catch(e) {// catch errors here
}

如何转换ByteDataList<int>

经过自我调查,解决方案是:

  1. .cast<int>()
ByteData audioByteData = await rootBundle.load(audioAssetsFullPath);
Uint8List audioUint8List = audioByteData.buffer.asUint8List(audioByteData.offsetInBytes, audioByteData.lengthInBytes);
List<int> audioListInt = audioUint8List.cast<int>();

或 2. 使用 .map

ByteData audioByteData = await rootBundle.load(audioAssetsFullPath);
Uint8List audioUint8List = audioByteData.buffer.asUint8List(audioByteData.offsetInBytes, audioByteData.lengthInBytes);
List<int> audioListInt = audioUint8List.map((eachUint8) => eachUint8.toInt()).toList();

encoding - 如何将 Dart 的ByteData转换为字符串?相关推荐

  1. c语言 utf 8转字符串,如何将UTF-8字节[]转换为字符串?

    我有一个byte[]数组,它是从一个我所知道的包含UTF-8的文件中加载的. 在一些调试代码中,我需要将其转换为字符串. 是否有一个班轮可以做到这一点? 在幕后 ,它应该只是一个分配和一个内存复制 , ...

  2. python强转字符串_在Python 3中将Exception转换为字符串

    在Python 3中将Exception转换为字符串 有谁知道,为什么这个Python 3.2代码 try: raise Exception('X') except Exception as e: p ...

  3. python 字节字符串_Python字符串转换为字节,字节转换为字符串

    python 字节字符串 Python字符串到字节 (Python String to bytes) Python String to bytes conversion can be done in ...

  4. 如何将字节数组转换为字符串[duplicate]

    本文翻译自:How to convert byte array to string [duplicate] This question already has an answer here: 这个问题 ...

  5. php类对象无法转还子符串问题,php – 类stdClass的对象无法转换为字符串

    我目前遇到PHP问题,我收到此错误, 无法将类stdClass的对象转换为字符串,当我在我的站点中运行此部分代码时,会发生错误, function myaccount() { $data['user_ ...

  6. 「译」在JavaScript中将值转换为字符串的5种方法

    原文: 5 Ways to Convert a Value to String in JavaScript 如果您关注Airbnb的样式指南,首选方法是使用"String()"? ...

  7. [置顶] Android面试题目之二:整形转换为字符串

    整形转换为字符串,写了五种算法,并进行了性能分析: 1. 算法: 直接先求得长度,然后从左向右求得各位的大小 private static class Int2String1 implements I ...

  8. Android面试题目之二:整形转换为字符串

    整形转换为字符串,写了五种算法,并进行了性能分析: 1. 算法: 直接先求得长度,然后从左向右求得各位的大小 private static class Int2String1 implements I ...

  9. 如何将Swift数组转换为字符串?

    本文翻译自:How do I convert a Swift Array to a String? I know how to programmatically do it, but I'm sure ...

最新文章

  1. AutoMocker单元测试
  2. mysql 定长文件_mysql 定时备份文件 参考文档
  3. MySQL的Limit详解(转载)
  4. 深入WPF中的图像画刷(ImageBrush)之1——ImageBrush使用举例
  5. Vue.js 2 渐进式前端框架 的最佳学习方法
  6. rust怎么拆自己石墙_房屋征拆:遭遇非法强拆怎么办?如何依法维护自己的权益...
  7. php 命令行 php.ini,php web环境和命令行环境下查找php.ini的方法分享
  8. 李飞飞点赞!,刚刚公布了第一批数据集benchmark入围名单
  9. netstat php,netstat查看网络端口情况
  10. 世界八大最顶尖的工业软件强国
  11. VS2015卸载。非常干净-亲测有用
  12. 硬盘接口类型简洁区别及SCSI设备和SCSI磁盘的概念区别
  13. C++智能指针与类型转换
  14. Springboot(大总结)
  15. Stimulsoft Reports如何进行数据连接
  16. 没有学历没有工作经验的程序员怎么找工作
  17. JS中的pipe原理
  18. Mac安装指定版本的node
  19. 58同城python_Python实战计划爬虫作业第一周作业:爬58同城
  20. 腾讯企业 html邮件模板,python连接腾讯企业邮箱发送html邮件

热门文章

  1. 怎么把php的文件链接成html,怎么把动态的php文件转换成静态的html文件,html文件是php文件…...
  2. mysql 插入数据 自增长_MySQL ------ 插入数据(INSERT和insert select)(二十)
  3. django弹出对话框_Django实现简单网页弹出警告代码
  4. 天气预报c是什么意思_大雪节气将至,为什么老话说:寒风迎大雪,三九天气暖?...
  5. linux中负载值为多少正常_Linux load average负载量分析与解决思路
  6. mysql防注入原理_MyBatis如何防止SQL注入
  7. pythondocx文档重叠_使用PYTHON实现docx文档的读写
  8. python双循环zip_Python如何同时进行两个循环
  9. python 判断图片是否损坏_检查图片是否损坏、图片后缀是否与实际图片类型对应 - Python...
  10. python 文件保存读取时不用with的问题