文件API - 进制转换 - 使用Javascript [英] File API - HEX conversion - Javascript

查看:161
本文介绍了文件API - 进制转换 - 使用Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取文件API的帮助本地文本文件,并使用了类似的功能,将其转换为十六进制文件BIN2HEX()(使用字符codeAT()函数),然后最后处理十六进制数字,以获得我的结果。所有这一切都在Javascript。

I am trying to read a local text file with the help of the File API, and convert it to an HEX file using a similar function to "bin2hex()" (using CharCodeAt() function), and then finally process the HEX numbers to obtain my results. All this in Javascript.

要我的文件转换为十六进制的数组,我扫描的文件中的每个字符通过一个for循环文件,然后使用BIN2HEX()函数来获取十六进制值。我希望在0x00到0xFF对应的任何性质我尝试转换的结果。但似乎有时候我获得没有明显的原因0xfffd或为0x00。有没有在其中的字符可以通过字符$ C $猫()函数或文件API读取处理方面一个限制?还是有可能更简单的方式做到这一点(PHP,阿贾克斯)?

To convert my file to an HEX array, I scan each character of the file via a for loop file and then use the bin2hex() function to obtain the HEX value. I would expect a result between 0x00 and 0xFF corresponding to whatever character I am trying to convert. But It seems that sometimes I am obtaining 0xfffd or 0x00 for no apparent reasons. Is there a limitations in terms of which characters you can process through the charcodeat() function or read with the File API? Or is there maybe easier way to do it (PHP, Ajax)?

非常感谢,

杰罗姆

推荐答案

直行进入的字节的,而不是通过的字符串

Go straight into Bytes rather than via String

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);

这篇关于文件API - 进制转换 - 使用Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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