如何将String转换为Hex nodejs

时间:2017-11-27 23:51:43

标签: node.js string hex

我正在寻找一些标准函数,比如hex to string但是反向, 我想将String转换为Hex,但我只发现了这个function ...

.task {
  margin: 10px 0;
  height:calc(100vh - 80px);
}

2 个答案:

答案 0 :(得分:2)

您可以使用以下功能:

  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'); 

答案 1 :(得分:1)

NPM amrhextotext,简单的文本转换为十六进制,十六进制转换为文本

安装

npm i amrhextotext

Usange:

const text = 'test text'
const hex = '746573742074657874'

const convert = require('amrhextotext')

//convert text to hex
let hexSample = convert.textToHex(text)
//Result: 746573742074657874

//Convert hex to text
let textSample = convert.hexToUtf8(hex)
//Result: test text
  

注释:页面来源