UTF-16LE txt文件在Flutter中解码为字符串(飞镖) [英] UTF-16LE txt file decode as String in Flutter (dart)

查看:127
本文介绍了UTF-16LE txt文件在Flutter中解码为字符串(飞镖)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要读取我正在使用的.txt文件的文件内容

To read a file contents of .txt file I am using

List<String> linesList = await file.readAsLines(encoding: latin1);
return linesList;

带有Encodng UTF-8的文件可以与上述代码完美配合. 但是对于UTF-16LE编码,它返回的列表的长度是文件中各行的两倍,但是除了第一行以外,它们都是空的.第一个索引包含ÿþ#

Files with Encodng UTF-8 are working perfectly with this above code. But for Encoding UTF-16LE its returning a list with length double of the lines in the file but are all empty except first line. This first index contains ÿþ#

推荐答案

第一个功劳归@​​Richard_Heap,他对上述问题发表了评论.他提到了一个可以对UTF格式进行编码和解码的dart程序包.使用此软件包,我已经能够按预期在Flutter应用中解码txt文件.

So the first credit goes to @Richard_Heap , who has commented on the above question. He mentioned a dart package that encodes and decodes UTF formats. I have been able to decode the txt files as expected in my Flutter app using this package.

首先,我要通过@Richard_Heap提到的软件包中提供的这些功能来识别utf格式类型

First I am identifying the utf format type by these functions available from the package mentioned by @Richard_Heap

 List<int> bytes = await file.readAsBytes();

hasUtf16beBom(bytes)
hasUtf16Bom(bytes)}
hasUtf16leBom(bytes)
hasUtf32beBom(bytes)
hasUtf32Bom(bytes)
hasUtf32leBom(bytes)

有不同的解码器&使用以上这些功能,一旦知道utf格式,便可以使用此程序包中的编码器功能.就像我用过

There are different decoder & encoder functions in this package that can be used once the utf format is known using these above functions. Like I used

String decodedString = decodeUtf16le(bytes);

这篇关于UTF-16LE txt文件在Flutter中解码为字符串(飞镖)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆