<p>If you also derive a <code>Read</code> instance, then it should be possible to use <code>show</code> and <code>read</code> to covert to and from <code>String</code> and there is likely a way of converting from <code>String</code> to and from <code>BuiltinByteString</code>.</p>

CC BY-SA 4.0
2023-01-11T19:54:42.927Z
2023-01-11T19:54:42.927Z
Erik de Castro Lopo
<p>I have a type defined as follows:</p>
<p><code>data A = A1 | B2 | A3 deriving (Show, Eq)</code></p>
<p>Is it possible to convert it to and from <code>BuiltinByteString</code> without explicitly having functions as such:</p>
<pre><code>convertToBuiltInByteString :: A-&gt; BuiltinByteString
convertToBuiltInByteString A1 = &quot;A1&quot;
convertToBuiltInByteString B2 = &quot;B2&quot;
convertToBuiltInByteString A3 = &quot;A3&quot;

fromBuiltInByteString :: BuiltinByteString -&gt; A
fromBuiltInByteString &quot;A1&quot; = A1
fromBuiltInByteString &quot;B2&quot; = B2
fromBuiltInByteString &quot;A3&quot; = A3
</code></pre>

<plutus>
Converting a sum type to BuiltinByteString
# Converting a sum type to BuiltinByteString

(Asked by: username_0 on 2023-01-11)

I have a type defined as follows:
data A = A1 | B2 | A3 deriving (Show, Eq)
Is it possible to convert it to and from BuiltinByteString without explicitly having functions as such:
convertToBuiltInByteString :: A-> BuiltinByteString
convertToBuiltInByteString A1 = "A1"
convertToBuiltInByteString B2 = "B2"
convertToBuiltInByteString A3 = "A3"

fromBuiltInByteString :: BuiltinByteString -> A
fromBuiltInByteString "A1" = A1
fromBuiltInByteString "B2" = B2
fromBuiltInByteString "A3" = A3

**Tags:** plutus

---

## 1 answers

### Answer by username_1 on 2023-01-11. Score: 0

If you also derive a `Read` instance, then it should be possible to use `show` and `read` to covert to and from `String` and there is likely a way of converting from `String` to and from `BuiltinByteString`.


---

