I'd like to keep an ID which is the first 32-bit of the hash function output in Solidity. I'm aware that "keccak256" output is of type:


which is more larger than I want.

Question: How can I truncate the output of hash function (i.e. keccak256) from "bytes32" to "bytes2"?

1 Answer 1


You can directly convert to bytes2 from bytes32, as follows:

function test() constant returns(bytes2){
        return bytes2(keccak256("123"));

Hope it helps~


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.