user1118: I am trying to find a way to inspect the base16 hash from a Tx on-chain using plutus.
I get a BuiltinByteString from the txInputs here:
getTxIdBBS :: TxInInfo -> BuiltinByteString
getTxIdBBS txInfo' = getTxId(txOutRefId (txInInfoOutRef txInfo'))

but then I need to get the base16 representation so I can compare and inspect the hex digits.  
 
Ideally, I would like to use a function similar to below, but this only works off-chain (due to conversion to non-builtin).
import qualified Data.ByteString.Base16 as B16 (encode)

txId    = getTxIdBBS (txInfo)
txId'   = fromBuiltin(txId)
txIdHex = toBuiltin(B16.encode txId')

I've also tried the following, but this does not work either.
import Data.Text.Encoding qualified as E
import PlutusTx.Builtins.Internal qualified as BI

{-# INLINABLE txEncodeUtf16 #-}
txEncodeUtf16 :: BI.BuiltinString -> BI.BuiltinByteString
txEncodeUtf16 (BI.BuiltinString s) = BI.BuiltinByteString $ E.encodeUtf16BE s

txHash = txEncodeUtf16(decodeUtf8(txId))

I am not able to use decodeUtf8 on a BuiltinByteString generated by sha2_256 and get the following error:
decodeUtf8*** Exception: Cannot decode byte '\xc6': Data.Text.Internal.Encoding.decodeUtf8: Invalid UTF-8 stream

I also can’t use encodeUtf16BE due to the following compiler error:
GHC Core to PLC plugin: E042:Error: Unsupported feature: Type constructor: GHC.Prim.ByteArray#

Any help or suggestions on how to convert the BuiltinByteString from getTxId into a BuiltinString  "on-chain" where I can inspect the hex digits would be awesome.
Thanks,
Lawrence

user1118: I was able to resolve this issue by converting (encoding) the BuiltinByteString from getTxId into a hex format.   From there I was able to inpsect the hex digits.   Below is an onchain encodeHex function that I used (thanks Minswap!).
-- Convert from a byte string to its hex (base16) representation. Example: [2, 14, 255] => "020eff"
{-# INLINEABLE encodeHex #-}
encodeHex :: BuiltinByteString -> BuiltinByteString
encodeHex input = go 0
  where
    len = lengthOfByteString input
    go :: Integer -> BuiltinByteString
    go i
      | i == len = emptyByteString
      | otherwise =
        consByteString (toChar $ byte `quotient` 16) $
          consByteString (toChar $ byte `remainder` 16) (go $ i + 1)
      where
        byte = indexByteString input i

        toChar :: Integer -> Integer
        toChar x
          -- 48 is ASCII code for '0'
          | x < 10 = x + 48
          -- 97 is ASCII code for 'a'
          -- x - 10 + 97 = x + 87
          | otherwise = x + 87


1118: <p>I am trying to find a way to inspect the base16 hash from a Tx on-chain using plutus.</p>
<p>I get a BuiltinByteString from the txInputs here:</p>
<pre><code>getTxIdBBS :: TxInInfo -&gt; BuiltinByteString
getTxIdBBS txInfo' = getTxId(txOutRefId (txInInfoOutRef txInfo'))
</code></pre>
<p>but then I need to get the base16 representation so I can compare and inspect the hex digits.  
 
Ideally, I would like to use a function similar to below, but this only works off-chain (due to conversion to non-builtin).</p>
<pre><code>import qualified Data.ByteString.Base16 as B16 (encode)

txId    = getTxIdBBS (txInfo)
txId'   = fromBuiltin(txId)
txIdHex = toBuiltin(B16.encode txId')
</code></pre>
<p>I've also tried the following, but this does not work either.</p>
<pre><code>import Data.Text.Encoding qualified as E
import PlutusTx.Builtins.Internal qualified as BI

{-# INLINABLE txEncodeUtf16 #-}
txEncodeUtf16 :: BI.BuiltinString -&gt; BI.BuiltinByteString
txEncodeUtf16 (BI.BuiltinString s) = BI.BuiltinByteString $ E.encodeUtf16BE s

txHash = txEncodeUtf16(decodeUtf8(txId))
</code></pre>
<p>I am not able to use decodeUtf8 on a BuiltinByteString generated by sha2_256 and get the following error:</p>
<pre><code>decodeUtf8*** Exception: Cannot decode byte '\xc6': Data.Text.Internal.Encoding.decodeUtf8: Invalid UTF-8 stream
</code></pre>
<p>I also can’t use encodeUtf16BE due to the following compiler error:</p>
<pre><code>GHC Core to PLC plugin: E042:Error: Unsupported feature: Type constructor: GHC.Prim.ByteArray#
</code></pre>
<p>Any help or suggestions on how to convert the BuiltinByteString from getTxId into a BuiltinString  &quot;on-chain&quot; where I can inspect the hex digits would be awesome.</p>
<p>Thanks,</p>
<p>Lawrence</p>
 1118: <p>I was able to resolve this issue by converting (encoding) the BuiltinByteString from getTxId into a hex format.   From there I was able to inpsect the hex digits.   Below is an onchain encodeHex function that I used (thanks Minswap!).</p>
<pre><code>-- Convert from a byte string to its hex (base16) representation. Example: [2, 14, 255] =&gt; &quot;020eff&quot;
{-# INLINEABLE encodeHex #-}
encodeHex :: BuiltinByteString -&gt; BuiltinByteString
encodeHex input = go 0
  where
    len = lengthOfByteString input
    go :: Integer -&gt; BuiltinByteString
    go i
      | i == len = emptyByteString
      | otherwise =
        consByteString (toChar $ byte `quotient` 16) $
          consByteString (toChar $ byte `remainder` 16) (go $ i + 1)
      where
        byte = indexByteString input i

        toChar :: Integer -&gt; Integer
        toChar x
          -- 48 is ASCII code for '0'
          | x &lt; 10 = x + 48
          -- 97 is ASCII code for 'a'
          -- x - 10 + 97 = x + 87
          | otherwise = x + 87
</code></pre>
