I am writing a emulator and I am trying to compare opcode logs from another working emulator. I have a log of executed opcodes ready to be compared it looks something like this
//log.txt (10000 lines long)
0
195
33
195
18...
I am reading this text file into a array using this code that looks kinda like this
//CPU.js
let log = fs.readFileSync("./log.txt").toString().split("\n"); // read file
let logCount = 0;
function executeOP(){
let opcode = memoryRead(PC.inc()); // get opcode
if(opcode != parseInt(log[logCount])){ // convert string to number and compare opcodes
console.log("failed opcode " + log[logCount]); // print out failed opcode
console.log("failed at line " + logCount); // print out failed opcode position
}
logCount++ // index of next opcode to compare
....
}
The problem is that parse in evaluates to NaN randomly on certain places in the test log array. It seems that extra invisible character example
console.log(log[2]) // 33
console.log(log[2][0]) // invisible
console.log(log[2].length) // 3? should be 2
I have tried correcting the array with (didn't work for me)
for(let i = 0; i < log.length; i++){
log[i] = log[i].trim();
}
// also
for(let i = 0; i < log.length; i++){
log[i] = log[i].replace(/(\r\n|\n|\r)/gm,"");
}
similar issue thread Loading text file in Javascript adds unexpected extra hidden characters
Thank you