diff --git a/lib/bn.js b/lib/bn.js index 3a4371e..ee78646 100644 --- a/lib/bn.js +++ b/lib/bn.js @@ -452,16 +452,16 @@ var w = this.words[i]; var word = (((w << off) | carry) & 0xffffff).toString(16); carry = (w >>> (24 - off)) & 0xffffff; - if (carry !== 0 || i !== this.length - 1) { - out = zeros[6 - word.length] + word + out; - } else { - out = word + out; - } off += 2; if (off >= 26) { off -= 26; i--; } + if (carry !== 0 || i !== this.length - 1) { + out = zeros[6 - word.length] + word + out; + } else { + out = word + out; + } } if (carry !== 0) { out = carry.toString(16) + out; diff --git a/test/utils-test.js b/test/utils-test.js index 8571905..d800873 100644 --- a/test/utils-test.js +++ b/test/utils-test.js @@ -5,6 +5,16 @@ var BN = require('../').BN; describe('BN.js/Utils', function () { describe('.toString()', function () { + describe('hex no padding', function () { + it('should have same length as input', function () { + var hex = '1'; + for (var i = 1; i <= 128; i++) { + var n = new BN(hex, 16); + assert.equal(n.toString(16).length, i); + hex = hex + '0'; + } + }); + }); describe('binary padding', function () { it('should have a length of 256', function () { var a = new BN(0);