peterolson / BigInteger.js

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

Number in base 2 or base 16 is wrong by +1 #100

Closed dadepo closed 7 years ago

dadepo commented 7 years ago

So I have this binary string

let binaryString = 100000000000010000110110111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001

Which I converted to bigInteger in base 2 like this:

let bigInteger = bigInt(num, 2)

When I now do bigInteger.toString() I was expecting 42540766411282592856903984951653826560' but I got42540766411282592856903984951653826561`

That is, the value is wrong by being one more than the value expected.

Also if I convert it to hexa string, the value is also off by 1.

That is when I do bigInteger.toString(16), I was expecting to get 20010db8000000000000000000000000 but got 20010db8000000000000000000000001

Is this a bug? Or I am the one doing something wrong? Thanks!

peterolson commented 7 years ago

I guess you figured out the issue already?

dadepo commented 7 years ago

Yup. I did. It was an error somewhere else which made me have the wrong expectation. Apologies for the trouble :)