S
I found an answer. The function in Python by default packs the four bytes in little endian format. JavaScript expects big endian. By merely changing
data = bytearray(struct.pack("f", vt))
to
data = bytearray(struct.pack(">f", vt))
the unpack at the other end was simple.
Java Script
function decodeFloat(data1) {
var binary = parseInt(data1, 16).toString(2);
if (binary.length < 32)
binary = ('00000000000000000000000000000000'+binary).substr(binary.length);
var sign = (binary.charAt(0) == '1')?-1:1;
var exponent = parseInt(binary.substr(1, 8), 2) - 127;
var significandBase = binary.substr(9);
var significandBin = '1'+significandBase;
var i = 0;
var val = 1;
var significand = 0;
if (exponent == -127) {
if (significandBase.indexOf('1') == -1)
return 0;
else {
exponent = -126;
significandBin = '0'+significandBase;
}
}
while (i < significandBin.length) {
significand += val * parseInt(significandBin.charAt(i));
val = val / 2;
i++;
}
return sign * significand * Math.pow(2, exponent);
}
// example use
data = '408df220' // is floating point value 4.43580627441406255
value = decodeFloat(data);
log.console('Value=', value);
The code above is from https://stackoverflow.com/questions/4414077/read-write-bytes-of-float-in-js
From a great website application at https://gregstoll.dyndns.org/~gregstoll/floattohex/ I produced the image below