androlo / standard-contracts

Storage repo for Solidity contracts, tests, and docs.
MIT License
98 stars 25 forks source link

Cast slice to bytesn #2

Open rainbreak opened 8 years ago

rainbreak commented 8 years ago

Thanks for your work on this - I'm looking forward to using it when it's stable.

Is it possible to enable functions like this:

function extract (bytes data, uint offset) returns (bytes4) {
    return bytes4(data.slice(offset, offset + 4))
}

i.e. returning a bytesn value type from a slice of bytes?

My current workaround feels clunky:

function extract (bytes data, uint offset) returns (bytes4) {
    uint32 ret = 0;
    for (uint32 i = 0; i < 4; i++) {
        ret += uint32(data[i + offset]) * 2 ** ((3 - i) * 8);
    }
    return (bytes4(ret));
} 
androlo commented 8 years ago

it requires 32 functions since it is 32 bytes types. There is one for byte, and one for bytes32 and some other types in the works (same as the RLP contract has).

Very sorry for the late reply, announcements were off for some reason.

rainbreak commented 8 years ago

No worries. Makes sense. Thanks!