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 know what I'm doing wrong, but I get 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[i];
}
return 4;
}
Node js
readFloat: function (offset, data) {
var b = new Buffer(4);
for (var i = 0; i < 4; i++) {
b[i] = 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 readFloatLE: 3.60133705331478e-43
const buf = Buffer.from([1,2,3,4,5,6,7,8]); buf.readDoubleBE(); // Returns: 8.20788039913184e-304 buf.readDoubleLE(); // Returns: 5.447603722011605e-270. But I don't know why. Curious myself.new Buffer(size). That is deprecated with the most recent version of node. It might technically be okay, since you're overwriting the 4 bytes anyways, but I wanted to bring it up.0x0102030405060708, and the doubles those represent are what you're seeing in your returns, based on whether you read them as big or little endian.