I used sha256() hash of a string and I got the following result 0x0de69f56365c10550d05e65ae8229dd0686f7894a807830daec8caa879731f4d for string Hello World2. Now I want to find out leading byte from the output of sha256() which is 'od' in this case.As the return type of sha256() is bytes32 how to read the leading bytes from the output. Here is the code I used to get sha256() of a string.

contract TestIntToString {
  function getSha256(string str) external pure returns (bytes32) {
    bytes32 hash = sha256(abi.encodePacked(str));

    return hash;
    //string y    = hash;

1 Answer 1


Since the sha256() function outputs a bytes32, which is is a fixed-size byte array you can easily access the leading byte of the hash, simply by returning hash[0].

  • How to check whether leading byte is all zeros. Jul 24, 2018 at 12:54
  • by returning hash[0] which is bytes1 it gives the output like this 0xod but I want only od Jul 24, 2018 at 12:56
  • Well, they are the same number, it is just a question of string representation. Jul 24, 2018 at 13:10
  • can you answer my comment1? Jul 24, 2018 at 13:25
  • Oh, yeah! regarding your first question: hash[0] == byte(0) as well as hash[0] == 0 will do the job. Jul 24, 2018 at 13:40

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.