peterolson / BigInteger.js

An arbitrary length integer library for Javascript
The Unlicense
1.12k stars 187 forks source link

Is there a convenient way to convert from/to an array of bytes? #159

Closed proehlen closed 5 years ago

proehlen commented 5 years ago

Love the library. Thanks for writing and sharing it.

Question: My application works with Uint8Arrays where each element is a number from 0x00 to 0xFF. Occasionally I need to convert these to BigInts to do some calculation and then back again. If I'm not mistaken, the toArray and fromArray methods would only work if my array elements were each 4 bits wide - ie max value 'F'. I can write a utility / wrapper to get around this but I wanted to check first that I didn't misunderstand those methods.

proehlen commented 5 years ago

Apologies, I was passing the wrong base. This works fine:

console.log(BigInt.fromArray([0xFF, 0xFF], 256, false).toString(16)) => "ffff"