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


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

  • 1
    I don't get it, 4c7b7ffb66b344fbaa64995af81e355a is already in hex format! Commented Nov 5, 2020 at 13:51
  • 1
    Besides that, a simple search on their official documentation page gives this. Commented Nov 5, 2020 at 13:53
  • You need the 0x Commented Nov 5, 2020 at 21:37
  • 2
    Is that for real??? Just add "0x" at the beginning of the string then! Commented Nov 6, 2020 at 6:30
  • My input varies, so just in case whatever input isn't already hexlified I want to make sure it always hexlifies. In this case yes, I could just append the 0x, plus, it makes my code more readable. Commented Nov 6, 2020 at 12:18

2 Answers 2


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.


Just as an addition, if someone is searching how to convert a non number string to hex, ethers can handle this as well:


Your Answer

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

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