//Write your own contracts here. Currently compiles using solc v0.4.15+commit.bbb8e64f. pragma solidity ^0.4.17; contract TestBits { function uintToByte() public pure returns(bytes){ bytes memory message = new bytes(32); uint value = 2**256-100000000000000000000000000000000000000000000000000000000000000000000000000; // bytes32(value) // 0xffc766e9d96c8c953ace5a5a70e044b48b9afbc7d3581c000000000000000000 // byte(bytes32(value)) // 0xff00000000000000000000000000000000000000000000000000000000000000 // byte(bytes32(value<<8)); // 0xc700000000000000000000000000000000000000000000000000000000000000 for(uint i=0; i<32; i++){ message[i] = byte(bytes32(value<<(8*i))); } // 0xffc766e9d96c8c953ace5a5a70e044b48b9afbc7d3581c000000000000000000 return message; } function addressToByte() public view returns(bytes){ bytes memory message = new bytes(20); address contractAddress = getContractAddress(); for(uint i=0; i<20; i++){ message[i] = byte(bytes20(bytes20(contractAddress) << (8*i))); } return message; } function anotherUintToBytes() public pure returns(bytes){ bytes memory message = new bytes(32); uint value = 26322820181848127205119429488744061657188852288741917040354512141634951523633; //0x3a32303138313133303030303030302b30383030000100005649500000412d31 for(uint i=0; i<32; i++){ message[i] = byte(bytes32(value<<(8*i))); } return message; } function anotherUintToBytes2() public pure returns(bytes32){ uint value = 26322820181848127205119429488744061657188852288741917040354512141634951523633; return bytes32(value); } function anotherUintToBytes3() public pure returns(bytes b){ b = new bytes(32); uint value = 26322820181848127205119429488744061657188852288741917040354512141634951523633; assembly { mstore(add(b, 32), value)} return b; } function uintArrayToBytes() public pure returns(byte){ uint256[] memory tickets = new uint256[](1); tickets[0] = 26322820181848127205119429488744061657188852288741917040354512141634951523633; bytes memory message = new bytes(32*tickets.length); // for(uint i=0; i<tickets.length; i++){ // message[i*32] = byte(bytes32(tickets[i])); // for(uint j=1; j<32; j++){ // message[i*32+j] = byte(tickets[i]=tickets[i]>>8); // } // } //tickets[0]>>8 //0x003a32303138313133303030303030302b30383030000100005649500000412d //byte(bytes32(tickets[0]>>8)) //0x00 //byte(bytes32(tickets[0]=tickets[0]>>8)) //0x00 //byte(tickets[0]=tickets[0]>>8) //0x2d //tickets[0]<<8 //0x32303138313133303030303030302b30383030000100005649500000412d3100 //byte(bytes32(tickets[0]<<8)); //0x32 //byte(bytes32(tickets[0]=tickets[0]<<8)) //0x32 //byte(tickets[0]=tickets[0]<<8) //0x00 return byte(tickets[0]=tickets[0]>>8); } function test() public pure returns(bytes32){ uint256[] memory tickets = new uint256[](1); tickets[0] = 26322820181848127205119429488744061657188852288741917040354512141634951523633; encodeMessage(tickets); return bytes32(tickets[0]); } function encodeMessage(uint256[] tickets) public pure returns(bytes){ bytes memory message = new bytes(tickets.length * 32); for (uint i = 0; i < tickets.length; i++){ message[i * 32] = byte(bytes32(tickets[i])); // convert uint256[] to bytes for (uint j = 1; j < 32; j++) { message[i * 32 + j] = byte(bytes32(tickets[i] = tickets[i] << 8)); } } } function correctArrayToBytes() public pure returns(bytes){ uint256[] memory tickets = new uint256[](1); tickets[0] = 26322820181848127205119429488744061657188852288741917040354512141634951523633; bytes memory message = new bytes(tickets.length * 32); for (uint i = 0; i < tickets.length; i++){ message[i * 32] = byte(bytes32(tickets[i])); // convert uint256[] to bytes for (uint j = 1; j < 32; j++) { message[i * 32 + j] = byte(bytes32(tickets[i] = tickets[i] << 8)); } } //0x3a32303138313133303030303030302b30383030000100005649500000412d31 //0x3a32303138313133303030303030302b30383030000100005649500000412d31 return message; } function getContractAddress() public view returns(address){ return this; } function getMaxUint() public pure returns(uint){ uint256 value = 2**256-1; return value; } function getERC721() public constant returns (bytes32) { return keccak256("onERC721Received(address,address,uint256,bytes)"); } }
0.4.18