In solidity, how can I convert bytes memory buffer to bytes8 type?
Example:
bytes8 buf2 = bytes8(memory_buf); // TypeError: Explicit type conversion not allowed
bytesmemorysolidity
In solidity, how can I convert bytes memory buffer to bytes8 type?
Example:
bytes8 buf2 = bytes8(memory_buf); // TypeError: Explicit type conversion not allowed
Best Answer
Using assembly it could look like this (copied and edited from this answer):
This is an older inefficient suggestion without using assembly, probably don't use this:
Basically it iterates over each of the first 8 bytes in the
inBytes
array, converts it to abytes8
variable, then shifts this variable to the right by an appropriate amount, then OR's the variable with the current result (which basically appends it, since the default value for theoutBytes8
is all zeros).I haven't tested either extensively but they seem to work.