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

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

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 工作才能从ByteData到List<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 anything
    return 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();


相关文章
|
11月前
|
Python
python 字符串 hex 互转
python 字符串 hex 互转
62 0
|
1月前
|
Go
Go字节数组与字符串相互转换
Go字节数组与字符串相互转换
33 3
Python__24--格式化字符串与字符串的编码、解码转换
驻留机制、格式化字符串与字符串的编码、解码转换
|
JSON Dart 数据格式
dart中json和对象互转
开发过程中,json是必不可少的基础技能之一。这里记录下,在Dart语言中,如何将json解析成实例对象,以及如何将实例对象转化成json字符串。
|
Java
Java中将base64编码字符串转换为图片
前一段时间,在做摄像头拍照上传,摄像头拍的照片为base64编码格式的字符串,需要上传至项目中,则需要使用到将base64编码字符串转换为图片
960 0
|
JavaScript
NodeJS:字符串和base64相互转换
NodeJS:字符串和base64相互转换
959 0
|
Java
Java工程编码格式由GBK转化成utf-8(编码格式互转)
Java工程编码格式由GBK转化成utf-8(编码格式互转)
472 0
Java工程编码格式由GBK转化成utf-8(编码格式互转)
|
JavaScript 前端开发 Python
Python 技术篇 - 使用unicode_escape对js的escape()方法编码后的字符串进行解码实例演示
Python 技术篇 - 使用unicode_escape对js的escape()方法编码后的字符串进行解码实例演示
237 0
Python 技术篇 - 使用unicode_escape对js的escape()方法编码后的字符串进行解码实例演示
|
JSON 数据格式 Python
Python字符串和json类型的相互转换实例演示,python字符串转json、json转字符串
Python字符串和json类型的相互转换实例演示,python字符串转json、json转字符串
315 0
Python字符串和json类型的相互转换实例演示,python字符串转json、json转字符串