0

The function keccak256 () takes a bytes variable. You can't pass it bytes3 or byte []; it has to be of type bytes.

How do you set that to a specific value?

Here are some things I've tried, and the error messages I got.

bytes memory a = 0x05;
Error: Type int_const 5 is not implicitly convertible to expected type bytes memory.

bytes memory b = bytes (0x05);
Error: Explicit type conversion not allowed from "int_const 5" to "bytes memory".

bytes1 c = 0x05;
bytes memory d = c;
Error: Type bytes1 is not implicitly convertible to expected type bytes memory.

bytes memory e = bytes (c);
Error: Explicit type conversion not allowed from "bytes1" to "bytes memory".

Is it even possible to create a variable of type bytes and set it to specific hex bytes?

1

bytes is essentially an array or reference type and you are trying to assign a primitive value to an array.

You can use assembly to store any value in bytes array.

function test() public pure returns (uint256 returnValue) {
    bytes memory b = new bytes(32);
    
    assembly {
        mstore(add(b, 32), 5)
        returnValue := mload(add(b, 32))
    }
} 

In the above code we initialize a bytes array with length 32. It can store an integer of size 256 bits in the 32 bytes it has. If we look at the content of variable b in memory, it will return 32 and that is the length of the array. The integer value is stored in the next 32 bytes.

Without using assembly you can only assign values to bytes array byte by byte using the index of the array. Like so,

bytes memory b = new bytes(1)
b[0] = 0x05;

Here, you have the bytes array with length 1 which is assigned a single byte value. And reading the content of b using mload(b) will return 1 and that is the length of array b here.

My answer is about why bytes assigning is not working. For the keccak hashing part of your question you can refer to Abhishek's answer.

Hope it helps!

| improve this answer | |
0

A simple solution to this can be ..
Using keccak256(abi.encodePacked(args))

This should work.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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