Solidity中uint转bytes
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]; } } }
文章来源:https://ethereum.stackexchange.com/questions/4170/how-to-convert-a-uint-to-bytes-in-solidity
posted on 2018-12-21 00:12 bijian1013 阅读(1437) 评论(0) 编辑 收藏 举报