从JavaScript的二进制文件读取的字节,没有jQuery的 [英] Read bytes from a binary file with JavaScript, without jQuery

查看:317
本文介绍了从JavaScript的二进制文件读取的字节,没有jQuery的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个JavaScript模拟器,我想它很轻,所以我不希望加载ROM中使用jQuery和jDataView。我思纯JS做了我自己的ROM加载器。

I'm trying to make a javascript emulator, and i'd like it to be very light, so I don't want to load the "ROMs" with jQuery and jDataView. Si I made my own ROM loader in pure JS.

它的工作原理pretty以及(在本网站由于许多话题),但仍然在IE浏览器的问题,为此,我找不到任何帮助别的地方。

It works pretty well (thanks to many topics on this site), but there's still a problem on IE, for which I couldn't find any help elsewhere.

下面是我的JS code:

Here's my JS code:

/**
* @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):

Here's my .htaccess (the chip8 ROMs have the extension .ch8):

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

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

And here's my test ROM (it contains the bytes 0x00 to 0xFF)

http://www.filedropper.com/test_16

测试的结果:

Firefox和Chrome做工精细:他们登录从0x00到0xFF的每个字节

Firefox and Chrome work fine: they log every byte from 0x00 to 0xFF

IE9不相同,除了0x80的之间的32个字节0x9F,这与风马牛不相及的数字代替。 (即使我们比较二进制codeS,没有明显的转换逻辑)

IE9 does the same, except for the 32 bytes between 0x80 to 0x9F, that are replaced with totally unrelated numbers. ( Even if we compare binary codes, there's no apparent conversion logic )

所以我的问题是:


  • 什么是IE9做这些字节
  • 我怎么能解决这个问题?

  • What does IE9 do with these bytes?
  • How could I fix it?

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

Thanks for your ideas (or solutions)!

最大

推荐答案

我终于找到了答案:


  • IE浏览器,不管你做什么这些字符,转换。

  • IE converts these characters, no matter what you do.

但它提供了一种直接出口的字节数组Ajax响应,这是比其他浏览器几乎冷却器

But it provides a way to export directly the AJAX response in a bytes array, and that's almost cooler than the other browsers

字节VAR =的VBArray(xhr.responseBody).toArray(); //只有在IE浏览器!

因此​​,这里的功能N将文件转换为字节数组,下降到IE7!

So here's the functionn to convert a file to a bytes array, down to 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
}

这篇关于从JavaScript的二进制文件读取的字节,没有jQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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