2

I am currently using it as follows.

bytes memory _byte = abi.encode(ca, tokenId);

I learned that this method can take different variables and produce the same results.

If I use the keccak256 function, I can prevent it, but I can't decode it.

Is there a way to have a unique value by encoding and decoding variables without using keccak256?

1 Answer 1

1

Here you go...

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract EncodeVars {

    bytes public _bytes;

    function encodeVars(address _addr, uint _num) external returns (bytes memory) {
        _bytes = abi.encode(_addr, _num);
        return _bytes;
    }

    function decodeVars() external view returns (address _addr, uint _num) {
        (_addr, _num) = abi.decode(_bytes, (address, uint));
    }

}

You're right not to use keccak256 as yes, that would give you a hash of the values.

Let me know if this answers your question.

1
  • Thank you for your help. But what I'm curious about is how you can combine multiple variables into one and keep their values unique. I know that the "abi.encode()" function can take a different input and make the same output, but am I mistaken? The reference is the following link: ethereum.stackexchange.com/questions/113188/…
    – Primrose
    Dec 13, 2022 at 0:38

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.