逐字节读取文件并解析为整数

6
我需要从一个文件中读取数据,这些数据被服务器逐字节地写入文件中。该文件具有固定的结构,现在我想使用JS读取其中的信息。
我找到了http://www.html5rocks.com/en/tutorials/file/dndfiles/并将其复制到fiddle中:http://jsfiddle.net/egLof4ph/
function readBlob(opt_startByte, opt_stopByte) {

    var files = document.getElementById('files').files;
    if (!files.length) {
        alert('Please select a file!');
        return;
    }

    var file = files[0];
    var start = parseInt(opt_startByte) || 0;
    var stop = parseInt(opt_stopByte) || file.size - 1;

    var reader = new FileReader();

    // If we use onloadend, we need to check the readyState.
    reader.onloadend = function(evt) {
        if (evt.target.readyState == FileReader.DONE) { // DONE == 2
            document.getElementById('byte_content').textContent = evt.target.result;
            document.getElementById('byte_range').textContent = ['Read bytes: ', start + 1, ' - ', stop + 1,
                ' of ', file.size, ' byte file'].join('');
        }
    };

    var blob = file.slice(start, stop);

var a = reader.readAsBinaryString(blob);

}

document.querySelector('.readBytesButtons').addEventListener('click', function(evt) {
    if (evt.target.tagName.toLowerCase() == 'button') {
        var startByte = evt.target.getAttribute('data-startbyte');
        var endByte = evt.target.getAttribute('data-endbyte');
        readBlob(startByte, endByte);
    }
}, false);

我知道前7个字节是无用的,可以抛弃。接下来的68个字节是一组,每个值都是4个字节大小。在这68个字节之后又有68个可用字节(这68个字节是“时间段”)。
我的问题: 当我使用这个代码时,我得到了许多符号(A、Q、&&&、特殊字符等),但实际上数据是长整型。如何将它们解析为数字?根据FileReader API,readAsBinaryString()返回原始二进制数据。如何正确地解析整个文件?
因此,原始文件看起来像这样:
    <7B>Metadata</7B><4B>long value</4B>....17times for each timeslot      <4B>long value</4B>....17times again.... and this util the end of the file.

当我使用上述代码时,输出结果如下:�&�&WK��。
此外,我发现了https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays(由于FileReader提供了一个返回ArrayBuffer的方法),所以我想我应该使用readAsArrayBuffer(),但是如何使用它来获取我的数据呢?

你有想要解析的文件示例吗? - Mauricio Poppe
很遗憾,我只有一个文件,可以看到原始数据的样子。我会在我的起始帖子中更新它。 - EsoMoa
你真的需要这个吗?为什么不在服务器上读取/处理文件并返回所需的整数? - Fals
1个回答

0

你真的需要二进制吗?

请注意,根据W3C的2012年7月12日工作草案,readAsBinaryString方法现已被弃用。

function readBlob(opt_startByte, opt_stopByte) {

    var files = document.getElementById('files').files;
    if (!files.length) {
      alert('Please select a file!');
      return;
    }

    var file = files[0];
    var start = parseInt(opt_startByte) || 0;
    var stop = parseInt(opt_stopByte) || file.size - 1;

    var reader = new FileReader();
    reader.onloadend = function (evt) {

      if (evt.target.readyState == FileReader.DONE) { // DONE == 2
        var a = new Uint8Array(evt.target.result)
        var binary = ""
        for (var i =0; i <= a.length; i++) {
          binary += Number(a[i]).toString(2)
        }
        document.getElementById('byte_content').textContent = binary;
        document.getElementById('byte_range').textContent = ['Read bytes: ', start + 1, ' - ', stop + 1,
          ' of ', file.size, ' byte file'].join('');
      }
    };;


    var blob = file.slice(start, stop);
    var a = reader.readAsArrayBuffer(blob)
  }

  document.querySelector('.readBytesButtons').addEventListener('click', function (evt) {
    if (evt.target.tagName.toLowerCase() == 'button') {
      var startByte = evt.target.getAttribute('data-startbyte');
      var endByte = evt.target.getAttribute('data-endbyte');
      readBlob(startByte, endByte);
    }
  }, false);

我不被允许触碰编码,所以无法改变它是二进制的事实。昨天我意识到readAs...确实没有返回任何东西,与我之前想的不同,我没有看到result属性。如果我使用Typed Array或DataView,有什么区别吗? - EsoMoa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接