1

Here is my bytes array: [28,2,1994]

Now, I want to create a function that parses each value and assigns it to its own uint256 variable.

Here is an example:

uint256 day = 28;

uint256 month = 2;

uint256 year = 1994;

That said, I have a function that converts an entire bytes array to a uint256:

 function bytesToUint(bytes memory data) private pure returns (uint256) {
        require(data.length <= 32, "Invalid data length");
        uint256 result = 0;
        for (uint256 index = 0; index < data.length; index++) {
            result = result << 8;
            result += uint8(data[index]);
        }
        return result;
    }

But I need this function to return 3 separate values that I can use in a separate function.

Any idea how to make this work?

1
  • Maybe you could explain the format of your bytes array a bit more. How are you inputting bytes memory into the function? 1994 is greater than 2^8 and thus doesn't fit into a uint8 size.
    – phaze
    Commented Dec 24, 2021 at 16:40

0

Your Answer

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

Browse other questions tagged or ask your own question.