In NodeJS, use Buffer to convert string to hex.
Buffer.from('hello world', 'utf8').toString('hex');
Simple example about how it works:
const bufferText = Buffer.from('hello world', 'utf8'); // or Buffer.from('hello world')
console.log(bufferText); // <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
const text = bufferText.toString('hex');
// To get hex
console.log(text); // 68656c6c6f20776f726c64
console.log(bufferText.toString()); // or toString('utf8')
// hello world
//one single line
Buffer.from('hello world').toString('hex')
Answer from Luis Estevez on Stack OverflowIn NodeJS, use Buffer to convert string to hex.
Buffer.from('hello world', 'utf8').toString('hex');
Simple example about how it works:
const bufferText = Buffer.from('hello world', 'utf8'); // or Buffer.from('hello world')
console.log(bufferText); // <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
const text = bufferText.toString('hex');
// To get hex
console.log(text); // 68656c6c6f20776f726c64
console.log(bufferText.toString()); // or toString('utf8')
// hello world
//one single line
Buffer.from('hello world').toString('hex')
You can use function like the below:
function stringToHex(str) {
//converting string into buffer
let bufStr = Buffer.from(str, 'utf8');
//with buffer, you can convert it into hex with following code
return bufStr.toString('hex');
}
stringToHex('some string here');
» npm install to-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
» npm install string-hex
Here's a node.js specific approach, taking advantage of the the Buffer class provided by the node standard lib.
https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings
To get the byte (0-255) value:
Buffer.from('80', 'hex')[0];
// outputs 128
And to convert back:
Buffer.from([128]).toString('hex');
// outputs '80'
To convert to utf8:
Buffer.from('80', 'hex').toString('utf8');
You can make use of Number.prototype.toString and parseInt.
The key is to make use of the radix parameters to do the conversions for you.
var bytestring = Number('0x' + hexstring).toString(10); // '128'
parseInt(bytestring, 2).toString(16); // '80'
» npm install convert-hex
Converting from a String containing a hex number to decimal and back:
let num = "0xff";
console.log(Number(num)); // to decimal
console.log(`0x${Number(num).toString(16)}`); // to hex notation string
I think you're looking for something like this:
// Convert a string with hex notation to number
var somestring = "0xFF";
var n = parseInt(somestring);
console.log(n); // 255
// Convert a number to string with hex notation
var somenumber = 0xff;
var s = somenumber.toString(16);
console.log("0x" + s); // 0xff