use*_*029 2 javascript api hex file
我试图在 File API 的帮助下读取本地文本文件,并使用与“bin2hex()”类似的函数(使用 CharCodeAt() 函数)将其转换为 HEX 文件,然后最后将 HEX 数字处理为得到我的结果。所有这些都在 Javascript 中。
要将我的文件转换为 HEX 数组,我通过 for 循环文件扫描文件的每个字符,然后使用 bin2hex() 函数获取 HEX 值。我希望在 0x00 和 0xFF 之间的结果对应于我尝试转换的任何字符。但似乎有时我会无缘无故地获得 0xfffd 或 0x00。您可以通过 charcodeat() 函数处理哪些字符或使用 File API 读取哪些字符是否有限制?或者有没有更简单的方法(PHP,Ajax)?
非常感谢,
杰罗姆
直接进入字节而不是通过字符串
var file = new Blob(['hello world']); // your file
var fr = new FileReader();
fr.addEventListener('load', function () {
var u = new Uint8Array(this.result),
a = new Array(u.length),
i = u.length;
while (i--) // map to hex
a[i] = (u[i] < 16 ? '0' : '') + u[i].toString(16);
u = null; // free memory
console.log(a); // work with this
});
fr.readAsArrayBuffer(file);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3418 次 |
| 最近记录: |