For example,
I need to slice my bytes32 variable to chunks, and put result into 5 variables, saving the order, bytes2
, bytes2
, bytes2
, bytes2
, bytes8
, bytes1
(aka byte
), the order should be saved and I don't really care about the rest of bytes.
Is solidity has some tools for that? Or it is only possible with bit operations.
I found some difficulties working with this low-level types.
UPDATE:
So I did prepare this function which parses numBytes
bytes from source
starting from start
byte.
function bytesChunck(bytes32 source, uint start, uint numBytes) constant returns(uint _result){
uint counter = 0;
uint result;
for(uint i = 0; i < numBytes; i++) {
result += uint8(source[start + i]);
}
return result;
//
}
then I just convert result to needed bytes, like bytes8(result)
. But it seems to be overflowed, since if I have like 8 bytes it first bytes always zeros. Any ideas? 🙂
Thank you.
Best Answer
Your question is not clear enough. When you want to split 32 bytes into 5 variables that have a total length of
2+2+2+2+8+1 = 17
you should specify what bytes you want to be chunked (bytes from left or bytes from right).I wrote an example that solves your problem as far as I could understand it. Comment the answer - explain what you exactly need, and I'll change the code.