如何通过web3将地址类型转换为bytes32?

时间:2019-09-25 14:55:31

标签: ethereum solidity web3

我有合同,要求在bytes32中输入,因此我需要将地址转换为bytes32,但在web3中看不到此方法。

1 个答案:

答案 0 :(得分:2)

以太坊地址为20个字节,因此您将十六进制地址转换为字节,然后从左向右填充32个字节。

/**
 * Inserts string every nth row
 *
 * @customfunction
 * @param {Object[][]} arr Input Column
 * @param {number} every Nth row to insert
 * @param {string} str String to insert
 */
function insertText(arr, every, str) {
  for (
    var i = 0, j = 0;
    i < arr.length - 1 && arr.splice(i++, 0, [str + ++j]);
    i += every
  );
  return arr;
}
const arr = [ //Input col A1:A25 looks like this 
  ['Sunday'],
  ['Monday'],
  ['Tuesday'],
  ['Wednesday'],
  ['Thursday'],
  ['Friday'],
  ['Saturday'],
  ['10'],
  ['20'],
  ['30'],
  ['40'],
  ['50'],
  ['60'],
  ['70'],
  ['MERCURY'],
  ['MARS'],
  ['JUPITER'],
  ['VENUS'],
  ['SATURN'],
  ['EARTH'],
  ['NEPTUNE'],
  ['Mary'],
  ['John'],
  ['Paul'],
  ['Ann'],
];
console.log(insertText(arr, 7, 'Group'));