使用Chrome文件系统API时无法适应文件编码 [英] Can't fit file encoding when working with Chrome File System API

查看:129
本文介绍了使用Chrome文件系统API时无法适应文件编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要读取一个文件,其中包含一组在ASCII表格中移动了65的符号。这意味着,对于我打算做的每个符号:

I need to read a file which contains a group of symbols moved 65 in ASCII table. It means, for each symbol I am meant to do:

String.fromCharCode('¢'.charCodeAt(0)-65) // returns 'a'

但它根本不起作用。我已经让我的朋友使用Python输入相同的文件进行测试,他们得到了正确的结果。

But it is not working at all. I have asked friends of mine to do the test using Python inputting the same file and they got the correct result.

当我尝试使用Chrome文件系统执行相同的工作时它根本不起作用。
我无法找回预期的符号。我认为这是我的编码/ charset plataform的一个问题,但我无法弄清楚是什么以及如何修复它。

When I try to do the same work with Chrome File System it does not work at all. I can't get back the expected symbols. I think it is a problem with my encoding/charset plataform but I can't figure out what is and how fix it.

我试过用其他编码打开文件:

I have tried opening the file with other encoding:

var reader=new FileReader();

reader.readAsText(file, 'windows-1252'); // no success
reader.readAsText(file, 'ISO-8859-2'); // no success

感谢任何帮助

推荐答案

问题是,您的移位文本不再是 text readAsText 条件。尝试使用任何标准代码页阅读它都无法正常工作。

Problem is, your shifted text is no longer text by readAsText criteria. Trying to read it with any standard codepage is not going to work.

您应该使用 readAsArrayBuffer() ,将其解释为无符号8- bit int array,shift the bytes,然后将结果转换为字符串

var buf = new Uint8Array(reader.readAsArrayBuffer(file));
buf = buf.map((byte) => byte-65);
var string = new TextDecoder("ascii").decode(buf);

这篇关于使用Chrome文件系统API时无法适应文件编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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