1

Sir, If my uint is 10,how to convert 10 to '0x000000000000000000000000000000000000000000000000000000000000000a' by using Node.js? and reverse it from '0x000000000000000000000000000000000000000000000000000000000000000a' to uint 10 ?

2 Answers 2

2

Sir, If my uint is 10,how to convert 10 to '0x000000000000000000000000000000000000000000000000000000000000000a' by using Node.js?

You can use web3.toHex(number) to convert decimal number to hex then using web3.utils.padLeft("0x0abcdef", 64) to format the hex string to a 64 byte length hex

https://web3js.readthedocs.io/en/1.0/web3-utils.html#padleft

and reverse it from '0x000000000000000000000000000000000000000000000000000000000000000a' to uint 10 ?

just simply use web3.toDecimal() function to convert any hex string to decimal

0

Got it. My example is:

var a = 10
console.log(a); //output :10
var b = web3.utils.toHex(a);  
console.log(b); //output :0xa
var c = web3.utils.padLeft(b, 64);
console.log(c);  //output :0x000000000000000000000000000000000000000000000000000000000000000a
var d = web3.utils.toDecimal(c)
console.log(d); //output :10

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