I have an unsigned long in javascript that I’m trying to convert to a byte array (8 bytes obviously for the long.) Here is what I have so far:
var deviceId = parseFloat("353268001238563");
var bytes = new Array(7);
for(var k=0;k<8;k++) {
bytes[k] = value & (255);
value = value >> 8
}
return bytes;
Unfortunately the bytes generated are incorrect. Any idea what I’m doing wrong?
The
>>operator doesn’t seem to work with values that can’t be represented in a regular 32-bit signed integer as it will wrap.A hack solution might be to use regular division instead of a bit-shift: