One can read in the docs:
A
bytesis similar tobyte[], but it is packed tightly in calldata and memory.
What does this exactly mean? For instance, according to this link, why should we convert a bytes object to a bytes32 one using:
function bytesToBytes32(bytes b, uint offset) private pure returns (bytes32) {
bytes32 out;
for (uint i = 0; i < 32; i++) {
out |= bytes32(b[offset + i] & 0xFF) >> (i * 8);
}
return out;
}
and not using:
function bytesToBytes32(bytes b, uint offset) private pure returns (bytes32) {
bytes32 out;
for (uint i = 0; i < 32; i++) {
out[i] = b[offset + i];
}
return out;
}
Furthermore:
- Why isn't it possible to set an element of a
bytes32structure individually, but it is possible withbytes? - If I want to do some computations with bytes (litterally bytes, not the type
bytes) by considering them asuint8, will it work for both structures by simply castingb[i]touint8(b[i])? For instance, in order to get anuint256from these types, would the following code:
uint res;
for (uint i = 0; i < 32; i++) {
res += uint8(b[i]) * (256 ** i);
}
work, be b be a bytes object or a bytes32 one? Finally, are uint res = uint(b[i]); and uint res = uint(uint8(b[i])); two equivalent codes?