convert-hex包讲解

2018-07-14  本文已影响0人  沙漠中的猴
nodejs.jpg

简介

JavaScript component to convert to/from hex strings and byte arrays.

convert-hex 是一个把“hex字符串”与“byte数组”相互转换的工具。

安装

npm install convert-hex --save

使用方法

bytesToHex(bytes)

var convertHex = require('convert-hex')

var bytes = [0x34, 0x55, 0x1, 0xDF]
console.log(convertHex.bytesToHex(bytes)) //"345501df"

输入bytes类型的数据,返回的是hex string类型

hexToBytes(hexStr)

var hex = "000000e4bda0e5a5bd" //"0x" prefix is optional
console.log(convertHex.hexToBytes(hex)) //[0,0,0,228,189,160,229,165,189]

输入hex string类型的数据,返回的是int类型的数组

这里需要注意,如果我们想显示可视化的字符串。请先转化成Buffer再转化成string

var hex = "000000e4bda0e5a5bd" //"0x" prefix is optional
console.log(Buffer(convertHex.hexToBytes(hex)).toString()) //你好

上一篇 下一篇

猜你喜欢

热点阅读