1

I'm unable to convert Uint/bytes32 back to my struct Jury

Here is the struct im trying to convert to bytes32.

struct Jury {
    address userAddress; //20 bytes
    uint16 credibilityScore; //2 bytes
    uint32 count; //4 bytes
    bool exists; //1 bytes
}

From my understand, the struct should fit within Uint/bytes32 size.

The method that failed:

 function toJury(uint256 _data) public pure returns (Jury memory _jury) {
        bytes memory data = abi.encodePacked(_data);

        (
            _jury.userAddress,
            _jury.credibilityScore,
            _jury.count,
            _jury.exists
        ) = abi.decode(data, (address, uint16, uint32, bool));
    }

The method I've used to convert from Jury to Bytes/Uint:

  function toUint(Jury memory _jury) external pure returns (uint256) {
        bytes memory _bytes = abi.encodePacked(
            _jury.userAddress,
            _jury.credibilityScore,
            _jury.count,
            _jury.exists
        );
        bytes memory result = new bytes(32);
        for (uint16 i = 0; i < 32; i++) {
            if (i < _bytes.length) {
                result[i] = _bytes[i];
            } else {
                break;
            }
        }
        return uint256(bytes32(result));
    }

Note: updated code, paste incorrectly previously

2
  • I think you mistakenly pasted the same code twice - both the packing function that failed and the one following it are identical Commented Feb 10, 2023 at 6:57
  • 1
    Thanks, I've updated it
    – Peter Lee
    Commented Feb 10, 2023 at 8:13

2 Answers 2

1

You can split your last 32 bytes of params. enter image description here

0

At the end, I cast my data to uint256 then to bytes32 in order to perform bits shifting. Might not be the most efficient, but it was easy for me to understand.

 function juryToUint(Jury memory _jury) internal pure returns (uint) {
        bytes32 userAddressData = bytes32(uint256(uint160(_jury.userAddress)));
        bytes32 credibilityScoreData = bytes32(uint256(_jury.credibilityScore));
        bytes32 countData = bytes32(uint256(_jury.count));

        bytes32 userAddressBytes = userAddressData << 56;
        bytes32 credibilityScoreBytes = credibilityScoreData << 40;
        bytes32 countBytes = countData << 8;
        bytes32 existsBytes = bytes32(_jury.exists ? uint256(1) : uint256(0));

        return
            uint(
                userAddressBytes |
                    credibilityScoreBytes |
                    countBytes |
                    existsBytes
            );
    }
function uintToJury(
        uint256 _data
    ) internal pure returns (Jury memory jury) {
        bytes32 data = bytes32(_data);

        jury.userAddress = address(uint160(uint256((data >> 56))));
        jury.credibilityScore = uint16(uint256(data >> 40));
        jury.count = uint32(uint256(data >> 8));
        jury.exists = uint256(bytes32(_data & 0xff)) == 1;

        return jury;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.