我正在尝试制作一个 javascript 模拟器,我希望它非常轻便,所以我不想用 jQuery 和 jDataView 加载“ROM”。 Si 我用纯 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 编码和解码?
关于javascript - 使用 JavaScript 从二进制文件中读取字节,无需 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12114446/