Solidity中uint转bytes方法如下:

pragma solidity ^0.4.2;

contract Test {
    
    function toBytesNickJohnson(uint256 x) constant returns (bytes b) {
        b = new bytes(32);
        assembly { mstore(add(b, 32), x) }
    }

    function toBytesEth(uint256 x) constant returns (bytes b) {
        b = new bytes(32);
        for (uint i = 0; i < 32; i++) {
            b[i] = byte(uint8(x / (2**(8*(31 - i))))); 
        }
    }

    function toBytesNicolasMassart(uint256 x) constant returns (bytes c) {
        bytes32 b = bytes32(x);
        c = new bytes(32);
        for (uint i=0; i < 32; i++) {
            c[i] = b[i];
        }
    }
}

Solidity中uint转bytes

 

文章来源:https://ethereum.stackexchange.com/questions/4170/how-to-convert-a-uint-to-bytes-in-solidity

相关文章:

  • 2022-02-17
  • 2022-12-23
  • 2022-12-23
  • 2021-06-26
  • 2022-12-23
  • 2023-04-02
  • 2022-12-23
猜你喜欢
  • 2021-09-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2021-09-22
相关资源
相似解决方案