Home > Back-end >  Convert hexstring to bigint with out node.js dependency
Convert hexstring to bigint with out node.js dependency

Time:08-19

I am trying to convert a hex string to a BigInt.

I cannot use the default BigInt from node.js where it works just fine since i am using React-native.

I tried using big-integer but i get the error:

 Invalid integer

Basic example here: https://playcode.io/943259

Displayed is how the native BigInt converts a hex string successfully to a BigInt. Then you can comment out the second part where i import BigInt from big-integer and it will throw the error.

let hexString ='0x9c46e9ec68e9bd4fe1faaba294cba38a71aa177534cdd1b6c7dc0dbd0abd7a7';
console.log('HexString to BigInt', BigInt(hexString));
//uncomment to get the error
//import BigInt from 'big-integer'
//console.log('HexString to BigInt', BigInt(hexString));

CodePudding user response:

The package big-integer cannot detect the radix from the string, but you can specify it

import BigInt from 'big-integer'
let hexString ='0x9c46e9ec68e9bd4fe1faaba294cba38a71aa177534cdd1b6c7dc0dbd0abd7a7';
console.log('HexString to BigInt', BigInt(hexString.substring(2), 16));
  • Related