I need to read each of the 256 bits stored in a bytes32 variable. I'm using Solidity 8.1
Thank you for any help you can offer
I need to read each of the 256 bits stored in a bytes32 variable. I'm using Solidity 8.1
Thank you for any help you can offer
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Test {
// 1 byte is enough to hold about 1 typed character, 4 x "abcdefgh"
bytes32 public test = "abcdefghabcdefghabcdefghabcdefgh";
function accessByteByIndex(uint index) public view returns(bytes1) {
// since you need 32 bytes
require(index<32,"Index out of range");
return test[index];
}
function reverseBytes() public pure returns(bytes memory) {
uint num = 32;
bytes memory chars = new bytes(num);
bytes memory reverse = new bytes(num);
chars = "abcdefghabcdefghabcdefghabcdefgh";
uint i = 0;
for (i = 0; i < num; i++) {
reverse[i] = chars[num - 1 - i];
}
return reverse;
}
}
needed to cast the bytes32 variable to a uint256 then use the following function:
function isBitSet(uint256 b, uint256 pos) public pure returns (bool) {
return ((b >> pos) & 1) == 1;
}