<p>Since <code>BuiltinData</code> derives <code>Show</code>:</p>
<pre><code>newtype BuiltinData = BuiltinData PLC.Data
    deriving newtype (Show, Eq, Ord)
</code></pre>
<p>and there's the function:</p>
<p><code>stringToBuiltinString :: String -&gt; BuiltinString</code> found here in <code>PlutusTx/Builtins/Class.hs</code></p>
<p>Assuming your &quot;variable name&quot; is <code>dummyBuiltinData</code>, you can convert <code>BuiltinData</code> to <code>BuiltinString</code> using:</p>
<p><code>stringToBuiltinString $ show dummyBuiltinData</code></p>
<p>EDIT:</p>
<p>As noted by <a href="https://cardano.stackexchange.com/users/2681/kolam">kolam</a>, <code>show</code> isn't <code>INLINABLE</code> and isn't applicable on-chain. Here is a version that should work:</p>
<p><code>decodeUtf8 $ unsafeDataAsB dummyBuiltinData</code></p>
<p>both <code>decodeUtf8</code> and <code>unsafeDataAsB</code> can be found in module <code>PlutusTx.Builtins.Internal</code></p>

CC BY-SA 4.0
2021-08-24T14:14:31.877Z
That won't work as `show` is not an inlinable function and thus cannot be used in the validation (on-chain) part of Plutus.

Those functions you mentioned are meant to be used in the off-chain part of Plutus.
kolam
CC BY-SA 4.0
2022-05-02T04:45:59.577Z
Yes, as @kolam says, you can't use show onchain to print out strings that are built at runtime from onchain code. I think this is because the script's address is derived from the hash of the validator function/onchain code, so it can only use hardcoded strings in traceError or traceIfFalse. -> I did not have any luck getting decodeUtf8 to run on chain either. I'm guessing that's because the hash I was attempting to print out didn't resolve to utf8.
marcel_g
CC BY-SA 4.0
2021-08-24T13:22:50.653Z
2021-08-25T09:12:22.863Z
2021-08-25T09:12:22.863Z
Aoaddeola
<p>You can't print out strings that are built at runtime in onchain code with Plutus, my guess is that's because the script's address is generated by hashing the validator function, so it only allows hardcoded strings so the hash of that function is always the same.</p>
<p>My solution is to trigger traceIfFalse with a function that gives me a clue about the variable I'm investigating:</p>
<pre><code>... 
mkValidator = traceIfFalse getTraceString False 

getTraceString :: BuiltinString
getTraceString = case someVar of 
                   a -&gt; &quot;it's a&quot;
                   b -&gt; &quot;it's b&quot;
                   _ -&gt; &quot;it's something else&quot;
...
</code></pre>
<p>A bit kludgy, but it's working for me.</p>

CC BY-SA 4.0
2022-05-02T04:54:18.983Z
2022-05-02T04:54:18.983Z
marcel_g
<p>Since I just started to write Plutus code, I want to make some tests of Plutus.</p>
<p>For example, I'd like to know what exactly my <code>redeemer :: BuiltinData</code> value is. However I can't convert BuiltinData to BuiltinString which means I can't use <code>trace :: BuiltinString -&gt; a -&gt; a</code> to log it.</p>
<p>Is it possible to convert BuiltinData to BuiltinString or there is different approach to log redeemer?</p>

<plutus>
How to convert BuiltinData to BuiltinString?
# How to convert BuiltinData to BuiltinString?

(Asked by: username_0 on 2021-08-24)

Since I just started to write Plutus code, I want to make some tests of Plutus.

For example, I'd like to know what exactly my `redeemer :: BuiltinData` value is. However I can't convert BuiltinData to BuiltinString which means I can't use `trace :: BuiltinString -> a -> a` to log it.

Is it possible to convert BuiltinData to BuiltinString or there is different approach to log redeemer?

**Tags:** plutus

---

## 2 answers

### Answer by username_1 on 2021-08-24. Score: 2

Since BuiltinData derives Show:
newtype BuiltinData = BuiltinData PLC.Data
    deriving newtype (Show, Eq, Ord)

and there's the function:
stringToBuiltinString :: String -> BuiltinString found here in PlutusTx/Builtins/Class.hs
Assuming your "variable name" is dummyBuiltinData, you can convert BuiltinData to BuiltinString using:
stringToBuiltinString $ show dummyBuiltinData
EDIT:
As noted by kolam, show isn't INLINABLE and isn't applicable on-chain. Here is a version that should work:
decodeUtf8 $ unsafeDataAsB dummyBuiltinData
both decodeUtf8 and unsafeDataAsB can be found in module PlutusTx.Builtins.Internal

> **Comments:**
> **username_2**: That won't work as `show` is not an inlinable function and thus cannot be used in the validation (on-chain) part of Plutus.  Those functions you mentioned are meant to be used in the off-chain part of Plutus.  
> **username_3**: Yes, as @kolam says, you can't use show onchain to print out strings that are built at runtime from onchain code. I think this is because the script's address is derived from the hash of the validator function/onchain code, so it can only use hardcoded strings in traceError or traceIfFalse. -> I did not have any luck getting decodeUtf8 to run on chain either. I'm guessing that's because the hash I was attempting to print out didn't resolve to utf8.  

---

### Answer by username_3 on 2022-05-02. Score: 0

You can't print out strings that are built at runtime in onchain code with Plutus, my guess is that's because the script's address is generated by hashing the validator function, so it only allows hardcoded strings so the hash of that function is always the same.
My solution is to trigger traceIfFalse with a function that gives me a clue about the variable I'm investigating:
... 
mkValidator = traceIfFalse getTraceString False 

getTraceString :: BuiltinString
getTraceString = case someVar of 
                   a -> "it's a"
                   b -> "it's b"
                   _ -> "it's something else"
...

A bit kludgy, but it's working for me.


---

