Demion
Demion

Reputation: 867

float to byte (JavaScript)

In function is transmited double.

function writebyte(id, data)
{
    data = Math.floor(data);
    buf[id] = String.fromCharCode(data & (255));
}

It works ok for 0-127 values. But with negative or > 127 works wrong.

128 = 0xC2 0x80 (should be 0x80 1 byte)
-1  = 0xC3 0xBF (should be 0xFF 1 byte)

So I think problem is function String.fromCharCode with parameter 128++ or negative.

Is there any way writing bytes to array directly without String.fromCharCode?

Upvotes: 3

Views: 1684

Answers (1)

Alnitak
Alnitak

Reputation: 339816

In this answer you will find JavaScript code that will convert from (hex) bytes into a double. [JS doesn't have "floats"]

Reversing the process is left as an exercise for the reader...

Upvotes: 1

Related Questions