【发布时间】:2016-06-20 16:28:57
【问题描述】:
我正在尝试使用TextEncoder 和TextDecoder 编写文件。我还需要将 65 与 ascii 表相加,并且在处理换行符时不要求和。我已经调整了here 提出的解决方案来使用文件 API 读取文件。但是我在处理编码时遇到了一些问题。
// write cames from fileEntry.createWriter
var result='0'+String.fromCharCode(124)+'1234'+String.fromCharCode(10); // 0|1234
var asciiArray=[];
var stringArray=[];
var fileContent='';
var tpmBuffer;
var uint8array=new TextEncoder().encode(result); // returns a Uint8Array containing the text given in parameters encoded
uint8array=uint8array.map((byte)=>byte+65); // shift :)
for(var i=0;i<uint8array.length;i++) {
if(uint8array[i]!==75) {
asciiArray.push(uint8array[i]);
} else {
// I cant shift line break!
asciiArray.push(10);
tpmBuffer= new TextEncoder().encode(String.fromCharCode.apply(null,asciiArray));
stringArray.push(new TextDecoder("utf-8").decode(tpmBuffer));
console.log(stringArray); //["q½rstu\n"]
asciiArray=[];
}
}
var encodedBlob= new Blob(stringArray, {
encoding:'UTF-8',
type: 'text/plain;charset=UTF-8'
});
// writer.write(encodedBlob);
当我尝试阅读生成的内容时,我得到以下信息:
// Now we read the generated file content with:
// fileContent = "q½rstu\n"
var buf= new Uint8Array(fileContent);
buf=buf.map((byte)=>byte-65);
var fileAsString= new TextDecoder("ascii").decode(buf);
/*
output bellow is given by console.log(fileAsString[i], fileAsString.charCodeAt(i));
0 48
129 -> Why this guy appers?
| 124
1 49
2 50
3 51
4 52
*/
如果我在构建字符串时没有出现这个129 元素,为什么我在读取文件内容时会出现它?
【问题讨论】:
标签: javascript encoding google-chrome-app fileapi