使用JavaScript从二进制文件中读取字节,而不使用jQuery
Read bytes from a binary file with JavaScript, without jQuery
我正在尝试制作一个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
}
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 如何使用jQuery自动打开页面上的所有链接
- 如何使用jquery在填充自动完成的值后使文本框只读
- jQuery:循环一个具有不同超时值的循环
- 用程序搜索JQuery数据表中的文本
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- jQuery匹配JSON对象的部分文本
- Jquery POST未填充数组
- JQuery使计数器每次更改时都会增加
- 如何在Angular2中使用jQuery插件
- 提高JQuery的性能
- 无法在通过jQuery的ajax加载的页面中执行javascript
- JQuery合并了keyup和focusout两个函数
- 如何使用jQuery选择下拉列表的值
- 使用JavaScript从二进制文件中读取字节,而不使用jQuery
- 将从jquery接收的文件转换为字节数组
- C/SSL/JQuery.ajax() 客户端 - >服务器连接重置,但发送了 1 个字节
- 仅使用 jQuery .load() 接收图像的字节码
- JavaScript字节字符串和jQuery AJAX
- 在javascript/Jquery中解密字节数据