使用JavaScript从二进制文件中读取字节,而不使用jQuery

Read bytes from a binary file with JavaScript, without jQuery

本文关键字:jQuery 字节 JavaScript 二进制文件 读取 使用      更新时间:2023-09-26

我正在尝试制作一个javascript模拟器,我希望它非常轻,所以我不想用jQuery和jDataView加载"ROM"。我用纯JS制作了自己的ROM加载程序。

它运行得很好(感谢这个网站上的许多主题),但IE仍然存在问题,我在其他地方找不到任何帮助。

这是我的JS代码:

/**
* @param file - the path or URL of the ROM file to load. The file must be served with the Mime-Type: 'text/plain; charset=x-user-defined'
* @param callback - a function to call when the loading is complete. Its first parameter contains an array of numbers representing the ROM bytes.
*/
function loadRom(file, callback)
{
  var xhr = new XMLHttpRequest();                             // AJAX loader
  xhr.onreadystatechange = function(){
    if(xhr.readyState == 4){                                  // When the file content is received
      var str = xhr.responseText;                             // Store it as a string
      var ch, bytes = [];
      for (var i = 0; i < str.length; i++){
        ch = str.charCodeAt(i);                               // Read each character
        bytes.push(ch & 0xFF);                                // Store the last byte of the character
      }
      callback(bytes);                                        // Call the callback function with bytes as parameter
    }
  };
  xhr.open("GET", file, true);                                // Load the file
  xhr.send(null);                                             // Send the AJAX request
}
// Test
var mem=[];
loadRom("TEST.ch8", function(m){
  for(i=0;i<m.length;i++)console.log(m[i].toString(16))                 // Log each byte in hexa
});

这是我的.htaccess(chip8 ROM有扩展名.ch8):

AddType 'text/plain; charset=x-user-defined' ch8

这是我的测试ROM(它包含0x00到0xFF字节)

http://www.filedropper.com/test_16

测试结果:

Firefox和Chrome运行良好:它们记录从0x00到0xFF 的每个字节

IE9也做同样的事情,只是0x80到0x9F之间的32个字节被完全不相关的数字所取代。(即使我们比较二进制代码,也没有明显的转换逻辑)

所以我的问题是:

  • IE9如何处理这些字节
  • 我该怎么修

感谢您的想法(或解决方案)!

最大。

您是否考虑过在客户端对数据进行base-64编码和解码?

我终于找到了答案:

  • 无论你做什么,IE都会转换这些字符。

  • 但它提供了一种直接导出字节数组中AJAX响应的方法,这几乎比其他浏览器更酷

    var bytes=VBArray(xhr.responseBody).toArray();//仅在IE上

下面是将文件转换为字节数组的函数,一直到IE7!

function loadRom(path, memory, callback)
{
  var i = 0,                                                                                        // Loop iterator
      ie /*@cc_on=1@*/,                                                                             // IE detection with conditional compilation
      xhr = new XMLHttpRequest;                                                                     // XHR object
  xhr.onreadystatechange = function(){                                                              // When the XHR object state changes
    if(xhr.readyState > 3){                                                                         // When the file is received (readyState 4)
      for(xhr = ie ? VBArray(xhr.responseBody).toArray() : xhr.responseText; i < xhr.length; i++){  // Get the response text as a bytes array (on IE) or a string (on other browsers) and iterate
        memory.push(ie ? xhr[i] : xhr.charCodeAt(i) & 0xFF);                                        // Store in memory the byte (on IE) or the last byte of the character code (on other browsers)
      }
      callback()                                                                                    // Call the callback function
    }
  }
  xhr.open("GET", path);                                                                            // Load the file
  xhr.send()                                                                                        // Send the XHR request
}