user139306: When converting uint256 (the datatype used to represent hashes) to string in Bitcoin Core, the bytes are reversed. When debugging functional tests, this can create potential confusion when comparing hashes passed to script on the Python side and hashes naively printed from within Bitcoin Core's cpp.
Why are the bytes reversed when getting HexStr() for hashes in Bitcoin Core?

user139306: From sipa on IRC (2023-01-06):

jamesob: Probably dumb question, but why does uint256::ToString() (-> HexStr()) print out the blob in reversed byte order?
sipa: Because that's what it has always done, really.
sipa: And people expect txids and block hashes in hex notation to be reserved.
sipa: Historically, that was because txids/block hashes were 256-bit numbers constructed by interpreting the double-sha256 output as a little-endian integer, and then printing it out for human consumption in hex (where humans expect numbers in big endian).
sipa: But now uint256 is just a byte array wrapper, the behavior is maintained because block hashes and txids are always printed in reverse.
sipa: (block hashes are still interpreted a little-endian numbers for the purpose of comparing PoW to the target, but that's handled by the arith_uint256 class now)


139306: <p>When converting <code>uint256</code> (the datatype used to represent hashes) to string in Bitcoin Core, the <a href="https://github.com/bitcoin/bitcoin/blob/master/src/uint256.cpp#L19-L27" rel="noreferrer">bytes are reversed</a>. When debugging functional tests, this can create potential confusion when comparing hashes passed to script on the Python side and hashes naively printed from within Bitcoin Core's cpp.</p>
<p>Why are the bytes reversed when getting <code>HexStr()</code> for hashes in Bitcoin Core?</p>
 139306: <p>From sipa on IRC (2023-01-06):</p>
<blockquote>
<p>jamesob: Probably dumb question, but why does <code>uint256::ToString()</code> (-&gt; <code>HexStr()</code>) print out the blob in reversed byte order?</p>
<p>sipa: Because that's what it has always done, really.</p>
<p>sipa: And people expect txids and block hashes in hex notation to be reserved.</p>
<p>sipa: Historically, that was because txids/block hashes were 256-bit numbers constructed by interpreting the double-sha256 output as a little-endian integer, and then printing it out for human consumption in hex (where humans expect numbers in big endian).</p>
<p>sipa: But now uint256 is just a byte array wrapper, the behavior is maintained because block hashes and txids are always printed in reverse.</p>
<p>sipa: (block hashes are still interpreted a little-endian numbers for the purpose of comparing PoW to the target, but that's handled by the arith_uint256 class now)</p>
</blockquote>
