我一直在寻找一些标准函数,例如十六进制到字符串但取反, 我想将 String 转换为 Hex,但我只找到了这个 function ...
// Example of convert hex to String
hex.toString('utf-8')
最佳答案
在 NodeJS 中,使用 Buffer将字符串转换为十六进制。
Buffer.from('hello world', 'utf8').toString('hex');
有关其工作原理的简单示例:
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')
关于node.js - 如何将字符串转换为十六进制nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47521730/