1

My initial aim was to return structure from the function.

After web search I ended up with serialization of data structure into bytes array.

Now I try to figure out how each type is represented in bytes and what is the dependency to properly convert it into bytes and backwards.

uint address bytes32

Can you provide docs which explains the dependency between data type and its representation in bytes?

Conversion uint to bytes Conversion uint256 bytes

  • Solidity supports returning multiple values. Just return the values from the struct. – Adam Kipnis Jan 18 '18 at 6:16
  • I operate with array of structs. Simply enumeration of all values in return statement would not be a good solution – Gleichmut Jan 18 '18 at 6:22
1

You can use Seriality library. Seriality is a library for serializing and de-serializing all the Solidity types in a very efficient way which mostly written in solidity-assembly

1- By means of Seriality you can easily serialize and deserialize your variables, structs, arrays, tuples, ... and pass them through the contracts and libraries.

2- You can decouple your contract from libraries by serializing parameters into a byte array.

3- It also can be used as an alternative for RLP protocol in Solidity.

Here is a sample :

pragma solidity ^0.4.16;

import "./Seriality.sol";

contract SerialitySample is Seriality {

function sample() public returns(int8 n1, int24 n2, uint32 n3, int128 n4, address n5, address n6) {

    bytes memory buffer = new bytes(64);
    int8    out1 = -12;
    int24   out2 = 838860;
    uint32  out3 = 333333333;
    int128  out4 = -44444444444;
    address out5 = 0x15B7926835A7C2FD6D297E3ADECC5B45F7309F59;
    address out6 = 0x1CB5CF010E407AFC6249627BFD769D82D8DBBF71;

    // Serializing
    uint offset = 64;

    intToBytes(offset, out1, buffer);
    offset -= sizeOfInt(8);

    intToBytes(offset, out2, buffer);
    offset -= sizeOfUint(24);

    uintToBytes(offset, out3, buffer);
    offset -= sizeOfInt(32);

    intToBytes(offset, out4, buffer);
    offset -= sizeOfUint(128);

    addressToBytes(offset, out5, buffer);
    offset -= sizeOfAddress();

    addressToBytes(offset, out6, buffer);

    // Deserializing
    offset = 64; 

    n1 = bytesToInt8(offset, buffer);
    offset -= sizeOfInt(8);

    n2 = bytesToInt24(offset, buffer);
    offset -= sizeOfUint(24);

    n3 = bytesToUint8(offset, buffer);
    offset -= sizeOfInt(32);

    n4 = bytesToInt128(offset, buffer);
    offset -= sizeOfUint(128);

    n5 = bytesToAddress(offset, buffer);
    offset -= sizeOfAddress();

    n6 = bytesToAddress(offset, buffer);
}

output buffer:

1cb5cf010e407afc6249627bfd769d82d8dbbf7115b7926835a7c2fd6d297e3a

decc5b45f7309f59fffffffffffffffffffffff5a6e798e413de43550cccccf4


"1": "int8:     n1 -12",

"2": "int24:    n2 838860",

"3": "uint32:   n3 85",

"4": "int128:   n4 -44444444444",

"5": "address:  n5 0x15b7926835a7c2fd6d297e3adecc5b45f7309f59",

"6": "address:  n6 0x1cb5cf010e407afc6249627bfd769d82d8dbbf71"
0

You can go through this short article for serialization/deserialization in solidity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.