我試圖製作一個JavaScript模擬器,並且我希望它非常輕薄,所以我不想用jQuery加載「ROMs」 jDataView。我用純JS製作了我自己的ROM加載器。使用JavaScript從二進制文件中讀取字節,沒有jQuery
它工作得很好(感謝這個網站上的很多主題),但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的之間的32個字節0x9F,則與風馬牛不相及的數字代替。 (即使我們比較二進制代碼,也沒有明顯的轉換邏輯)
所以我的問題是:
- 是什麼IE9做這些字節?
- 我該如何解決?
感謝您的想法(或解決方案)!
最大。
我已經玩了一下你的代碼段爲我自己的目的,如果你不介意我這樣做,但遇到了一些問題:http://stackoverflow.com/questions/22576518 –