web3.toHex
优质
小牛编辑
131浏览
2023-12-01
web3.toHex
将任何值转为HEX
。
参数:
String|Number|Object|Array|BigNumber
- 需要转化为HEX
的值。如果是一个对象或数组类型,将会先用JSON.stringify
1进行转换成字符串。如果传入的是BigNumber
2,则将得到对应的Number
的HEX
。
示例:
//初始化基本对象
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var BigNumber = require('bignumber.js');
var str = "abcABC";
var obj = {abc: 'ABC'};
var bignumber = new BigNumber('12345678901234567890');
var hstr = web3.toHex(str);
var hobj = web3.toHex(obj);
var hbg = web3.toHex(bignumber);
console.log("Hex of Sring:" + hstr);
console.log("Hex of Object:" + hobj);
console.log("Hex of BigNumber:" + hbg);
$ node test.js
Hex of Sring:0x616263414243
Hex of Object:0x7b22616263223a22414243227d
Hex of BigNumber:0xab54a98ceb1f0ad2