Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

float to byte (JavaScript)

Tags:

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?

like image 564
Demion Avatar asked Mar 12 '26 13:03

Demion


1 Answers

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...

like image 106
Alnitak Avatar answered Mar 15 '26 01:03

Alnitak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!