Solidity – Understanding sha256() Output in Smart Contracts

hashhash-algorithmsolidity

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;
    }
}

Best Answer

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].