Skip to main content
Fixing a code error
Source Link

Here is a way to extract bytes32 out of a bytes array:

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;
}
 

Here is a way to extract bytes32 out of a bytes array:

function bytesToBytes32(bytes b, uint offset) private pure returns (bytes32) {
    bytes32 out;

    for (uint i = 0; i < 32; i++) {
        out |= ((b[offset + i] & 0xFF) << (i * 8));
    }
    return out;
}
 

Here is a way to extract bytes32 out of a bytes array:

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;
}
Source Link

Here is a way to extract bytes32 out of a bytes array:

function bytesToBytes32(bytes b, uint offset) private pure returns (bytes32) {
    bytes32 out;

    for (uint i = 0; i < 32; i++) {
        out |= ((b[offset + i] & 0xFF) << (i * 8));
    }
    return out;
}