1

How can I use ethers.js to get the token id (34797) from this 0x number?

0x0000000000000000000000000000000000000000000000000000000000034797

I'm not sure which util to use from ethers.js I've looked through the docs and Googled it.

3 Answers 3

2

Use BigNumber

hexText = "0x0000000000000000000000000000000000000000000000000000000000034797"
numb = new ethers.utils.BigNumber(hexText).toNumber()
console.log(numb)

JSFiddle example

1

No need for ethers.js, you just need to convert it from hexadecimal to decimal:

parseInt('0x34797',16)

214935

(which is different than 34797).

0
const { BigNumber } = require('ethers').utils;

const hexString = "0x0000000000000000000000000000000000000000000000000000000000034797";

const bigNumber = BigNumber.fromHexString(hexString);

const tokenId = bigNumber.toString();

console.log(tokenId); // "34797"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.