Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save anemochore/3277fefb9ecc70a3cfbe4772188ab257 to your computer and use it in GitHub Desktop.
Save anemochore/3277fefb9ecc70a3cfbe4772188ab257 to your computer and use it in GitHub Desktop.
// for http://stackoverflow.com/questions/42160903/
var myFile = File.openDialog('select file');
myFile.open("r");
myFile.encoding = "binary";
var buffers = [];
for(var x=0; x<myFile.length; x += 4) {
myFile.seek(x, 0);
var buffer = "0x";
for(var y=0; y<4; y++) {
var hex = myFile.readch().charCodeAt(0).toString(16);
if(hex.length === 1) {
hex = "0" + hex;
}
buffer += hex;
}
buffers.push(parseFloat2(buffer));
}
alert(buffers);
function parseFloat2(str) {
// from http://stackoverflow.com/a/14090278/6153990
var float2 = 0, sign, order, mantiss,exp,
int2 = 0, multi = 1;
if (/^0x/.exec(str)) {
int2 = parseInt(str,16);
} else {
for (var i = str.length -1; i >=0; i -= 1) {
if (str.charCodeAt(i)>255) {
console.log('Wrong string parametr');
return false;
}
int2 += str.charCodeAt(i) * multi;
multi *= 256;
}
}
sign = (int2>>>31)?-1:1;
exp = (int2 >>> 23 & 0xff) - 127;
mantiss = ((int2 & 0x7fffff) + 0x800000).toString(2);
for (i=0; i<mantiss.length; i+=1){
float2 += parseInt(mantiss[i])? Math.pow(2,exp):0;
exp--;
}
return float2*sign;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment