Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Javascript (but not Node), how do I divde two Uint8Arrays?

I'm using in-browser Javascript, not NodeJS. I have two Uint8Arrays ...

var d1 = new Uint8Array([255, 255, 255, 255, 255, 255, 255, 255])
var d2 = new Uint8Array([255, 255, 255, 255, 237, 49, 56, 0])

Each will have exactly 8 elements that are whole numbers between 0 and 255. Each array represents a larger number. For example, the first array represents the positive integer

0xffffffff

My question is how can I divide d1 by d2 and get a result? I read that the maximum value for an integer in Javascript is 2^53, which I believe less than the maximum number I could have. I don't care what the object type the result is, but Uint8Array is fine by me.

like image 565
satish Avatar asked Jun 05 '18 20:06

satish


2 Answers

There is a library you can use call BigInteger.. https://www.npmjs.com/package/big-integer

I didn't see a built in way to use Uint8Array, but I found this -> Javascript ArrayBuffer to Hex that had a way to convert into hex, that bigInteger seems ok with.

So here is an example of using it. ->

var d1 = new Uint8Array([255, 255, 255, 255, 255, 255, 255, 255]);
var d2 = new Uint8Array([255, 255, 255, 255, 237, 49, 56, 0]);

function buf2hex(buffer) { // buffer is an ArrayBuffer
  return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
}

var bd1 = bigInt(buf2hex(d1), 16);
console.log(`value 1 = ${bd1.toString()}`);
var bd2 = bigInt(buf2hex(d2), 16);
console.log(`value 2 = ${bd2.toString()}`);
var r = bd1.divmod(bd2);
console.log(`result ${r.quotient.value} remainder ${r.remainder.value}`);
<script src="https://peterolson.github.io/BigInteger.js/BigInteger.min.js"></script>
like image 140
Keith Avatar answered Oct 20 '22 18:10

Keith


Although max number would be:

8 bytes = 2^64-1 = 18446744073709551615 (check 2**64 in browser - the result will be different!)

which is greater than maximum safe integer:

Number.MAX_SAFE_INTEGER = 9007199254740991


I would try anyway something like this:

var d1 = new Uint8Array([255, 255, 255, 255, 255, 255, 255, 255]);
var d2 = new Uint8Array([255, 255, 255, 255, 237, 49, 56, 0]);

function decodeInt(uint8a){
    return parseInt('0x'+Array.from(uint8a).map(x=>('0'+x.toString(16)).slice(-2)).join(''));
}

decodeInt(d1) / decodeInt(d2);

EDIT

The results are obviously wrong if you are above Number.MAX_SAFE_INTEGER

like image 33
Michał Z. Avatar answered Oct 20 '22 18:10

Michał Z.