Solidity

如何將純文字字元串變數轉換為 bytes32 變數?以及如何在轉換後將 bytes32 儲存在一個變數中?

  • January 7, 2019
pragma solidity ^0.4.25;

contract Purchase {
   address public sellerAddress;

   constructor (address _sellerAddress) {
       sellerAddress = _sellerAddress;
   }
}

contract Buyer is Purchase {
   address public buyerAddress;
   string pass;
   bytes32 hashpass;

   modifier onlyBuyer() {
       require(msg.sender == buyerAddress);
       _;
   }

   constructor(string _pass) {
       buyerAddress = msg.sender;
       pass = _pass;
   }

   function stringToBytes32(string pass) returns (bytes32 hashpass) {
       hashpass = bytes32(pass);
   }

   function generateHashPass() public {

   }
}

如果要將字元串轉換為 bytes32,可以使用這篇文章中描述的方法:如何將字元串轉換為 bytes32?

但是,您似乎正在嘗試對字元串進行雜湊處理,這可以使用諸如keccak256.

hashpass = keccak256(pass);

引用自:https://ethereum.stackexchange.com/questions/65126