[Ethereum] Convert String to Hex using ethers.js

ethers.js

If I use web3.js I can convert a string to hex with:

web3.utils.toHex('4c7b7ffb66b344fbaa64995af81e355a')

How do I use the same utility for ethers.js?

Best Answer

I couldn't find it in their old documentation but if you check their legacy documentation https://docs.ethers.io/v4/api-utils.html#hex-strings you will find that you can convert any number, BigNumber, hex string or arrayish to hex with the following command:

ethers.utils.hexlify(5) which returns 0x05

Hope this solves your question.

Related Topic