Skip to content

Instantly share code, notes, and snippets.

@kig
Created December 17, 2009 11:13
Show Gist options
  • Select an option

  • Save kig/258685 to your computer and use it in GitHub Desktop.

Select an option

Save kig/258685 to your computer and use it in GitHub Desktop.
// read big-endian float
readFloat32 = function(data, offset) {
var b1 = data.charCodeAt(offset) & 0xFF,
b2 = data.charCodeAt(offset+1) & 0xFF,
b3 = data.charCodeAt(offset+2) & 0xFF,
b4 = data.charCodeAt(offset+3) & 0xFF;
var sign = 1 - (2*(b1 >> 7));
var exp = (((b1 << 1) & 0xff) | (b2 >> 7)) - 127;
var sig = ((b2 & 0x7f) << 16) | (b3 << 8) | b4;
if (sign == 1 && sig == 0 && exp == -127)
return 0.0;
return sign * (1 + sig * Math.pow(2, -23)) * Math.pow(2, exp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment