github.com/Tri-stone/burrow@v0.25.0/tests/jobs_fixtures/app39-sign-extension/byte_casting.sol (about) 1 pragma solidity >=0.0.0; 2 3 contract ByteCasting { 4 5 6 function Test(int8 _in1, int256 _in2, int16 _in3) public returns(int8 _out1, int256 _out2, int16 _out3) { 7 8 bytes memory _buff = new bytes(128); 9 10 11 // Serializing 12 uint128 _offst = 128; 13 14 int8ToBytes(_offst,_in1,_buff); 15 _offst -= 1; 16 17 int256ToBytes(_offst,_in2,_buff); 18 _offst -= 32; 19 20 int16ToBytes(_offst,_in3,_buff); 21 _offst -= 2; 22 23 // Deserializing 24 _offst = 128; 25 26 _out1 = bytesToInt8(_offst,_buff); 27 _offst -= 1; 28 29 _out2 = bytesToInt256(_offst,_buff); 30 _offst -= 32; 31 32 _out3 = bytesToInt16(_offst,_buff); 33 _offst -= 2; 34 35 } 36 37 38 function int8ToBytes(uint128 _offst, int8 _input, bytes memory _output) public { 39 40 assembly { 41 mstore(add(_output, _offst), _input) 42 } 43 } 44 45 function int16ToBytes(uint128 _offst, int16 _input, bytes memory _output) public { 46 47 assembly { 48 mstore(add(_output, _offst), _input) 49 } 50 } 51 52 function int256ToBytes(uint128 _offst, int256 _input, bytes memory _output) public { 53 54 assembly { 55 mstore(add(_output, _offst), _input) 56 } 57 } 58 59 60 function bytesToInt8(uint128 _offst, bytes memory _input) public returns (int8 _output) { 61 62 assembly { 63 _output := mload(add(_input, _offst)) 64 } 65 } 66 67 function bytesToInt16(uint128 _offst, bytes memory _input) public returns (int16 _output) { 68 69 assembly { 70 _output := mload(add(_input, _offst)) 71 } 72 } 73 74 function bytesToInt256(uint128 _offst, bytes memory _input) public returns (int256 _output) { 75 76 assembly { 77 _output := mload(add(_input, _offst)) 78 } 79 } 80 }