This code will show the data buffer as a hex string:
buff.toString('hex');
Answer from Seryh on Stack OverflowThis code will show the data buffer as a hex string:
buff.toString('hex');
Top answer is the simplest way to do it.
An alternative method:
data = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
Array.prototype.map.call(new Uint8Array(data),
x => ('00' + x.toString(16)).slice(-2))
.join('').match(/[a-fA-F0-9]{2}/g).reverse().join('');
In new versions of node (6+) the new Buffer() interface is deprecated. Use:
Buffer.from("250001000192CD0000002F6D6E742F72", "hex")
instead.
Find more info on this using this link below https://nodejs.org/api/buffer.html#buffer_static_method_buffer_from_string_encoding
new Buffer("250001000192CD0000002F6D6E742F72", "hex")
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
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