diff --git a/cpp/MGBigNumberHostObject.cpp b/cpp/MGBigNumberHostObject.cpp index d0ba69e..48d2f64 100644 --- a/cpp/MGBigNumberHostObject.cpp +++ b/cpp/MGBigNumberHostObject.cpp @@ -333,9 +333,16 @@ namespace margelo std::shared_ptr thiz = thisValue.getObject(runtime).getHostObject(runtime); int base = 10; - if (!arguments[0].isUndefined() && arguments[0].isNumber()) - { - base = (int)arguments[0].asNumber(); + if (!arguments[0].isUndefined()) { + if (arguments[0].isNumber()) { + base = static_cast(arguments[0].asNumber()); + } else if (arguments[0].isString()) { + std::string argValue = arguments[0].getString(runtime).utf8(runtime); + if (argValue == "hex") { + base = 16; + } + // TODO: Add more conditions here if you support other string representations for bases. + } } int len = -1; if (!arguments[1].isUndefined()) diff --git a/example/src/Testing/bn-tests/constructor-test.js b/example/src/Testing/bn-tests/constructor-test.js index 5d2393d..4c753f8 100644 --- a/example/src/Testing/bn-tests/constructor-test.js +++ b/example/src/Testing/bn-tests/constructor-test.js @@ -38,6 +38,11 @@ export function registerConstructorTests() { it('should accept two-limb LE number', function () { assert.equal(new BN(0x4123456, null, 'le').toString(16), '56341204'); }); + + it('should correctly convert number to hexadecimal', function () { + assert.equal(new BN(100).toString('hex'), '64'); + assert.equal(new BN(100).toString(16), '64'); + }); }); describe('with String input', function () {