Search code examples
javascriptarraysdivisionbigintuint8array

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.


Solution

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