Skip to content

How to convert BigNumber to bytes32? #2027

Answered by zemse
PaulRBerg asked this question in Q&A
Discussion options

You must be logged in to vote

You can use hexZeroPad.

const amount = ethers.BigNumber.from(1);
ethers.utils.hexZeroPad(amount.toHexString(), 32) // 0x0000000000000000000000000000000000000000000000000000000000000001

And yeah parseBytes32String, formatBytes32String are different, use case for them is to store strings in smart contracts as bytes32 instead of the dynamic length type string. But that puts a limit of 32 utf8 characters, which can be reasonable for things like name and symbol (interestingly MKR uses bytes32 for name and symbol, as it was deployed before ERC20 was finalized)

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
1 reply
@PaulRBerg
Comment options

Answer selected by PaulRBerg
Comment options

You must be logged in to vote
4 replies
@zemse
Comment options

zemse Mar 16, 2022
Collaborator

@aster2709
Comment options

@zemse
Comment options

zemse Mar 18, 2022
Collaborator

@aster2709
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants