Convert float to byte array and read with node js

Hello,

Can someone explain how I can convert a float(Vector3.x) to a byte array with c# and decode it with node js?

I read on the internet that Vector3.x is a system.single data type and use 4 bytes(32 bits). I use BitConverter to convert it to a byte array. With Nodejs I use readFloatBE().

I don`t what I do wrong, but I got constantly a bad result with node js with console.log().

Unity csharp:

        public static int FloatToBit(int offset, ref byte[] data, Single number)
        {
            byte[] byteArray = System.BitConverter.GetBytes(number);
           for (int i = 0;i<4;i++)
            {
                data[offset + i] = byteArray*;*

}

return 4;
}
Node js
readFloat: function (offset, data) {
var b = new Buffer(4);
for (var i = 0; i < 4; i++) {
b = data[offset + i];
}
return data.readFloatLE(b, 0);
},
If I send -2.5, unity output is: 0 0 32 191 with -1 unity output is: 0 0 128 192
Nodejs output with read:
3.60133705331478e-43

I never really used the Buffer object to read / convert float values, but shouldn’t you use:

return b.readFloatLE(0);

Just have a look at the documentation.

If “data” is a Buffer as well you should be able to just do this:

readFloat: function (offset, data) {
   return data.readFloatLE(offset);
},

readFloatLE already does what your readFloat method should do.