[英]Can't fit file encoding when writing a file with Chrome File System API
[英]Can't fit file encoding when working with Chrome File System API
我需要讀取一個文件,其中包含一組在ASCII表中移動65的符號。 這意味着,對於我要執行的每個符號:
String.fromCharCode('¢'.charCodeAt(0)-65) // returns 'a'
但這根本沒有用。 我請我的朋友使用Python輸入相同的文件進行測試,他們得到了正確的結果。
當我嘗試對Chrome File System執行相同的工作時,它根本無法工作。 我無法取回預期的符號。 我認為這與我的編碼/字符集平台有關,但我不知道是什么以及如何解決。
我嘗試用其他編碼打開文件:
var reader=new FileReader();
reader.readAsText(file, 'windows-1252'); // no success
reader.readAsText(file, 'ISO-8859-2'); // no success
感謝任何幫助
問題是,根據readAsText
條件,您轉移的文本不再是文本 。 嘗試使用任何標准代碼頁讀取它均行不通。
您應該使用readAsArrayBuffer()
將文件讀取為二進制文件,將其解釋為無符號的8位int數組,將字節移位,然后將結果轉換為string 。
var buf = new Uint8Array(reader.readAsArrayBuffer(file));
buf = buf.map((byte) => byte-65);
var string = new TextDecoder("ascii").decode(buf);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.