<p>This is how I've done the integer conversion in the past. Might need some minor updating:</p>
<pre><code>{-# INLINEABLE intToString #-}
intToString :: Integer -&gt; Builtins.String
intToString i = Prelude.foldr Builtins.appendString &quot;&quot; strings
  where
    ints = intToInts i
    strings = map (Builtins.charToString . intToChar) ints


{-# INLINEABLE intToChar #-}
intToChar :: Integer -&gt; Haskell.Char
intToChar i
  | (Builtins.equalsInteger i 0) = '0'
  | (Builtins.equalsInteger i 1) = '1'
  | (Builtins.equalsInteger i 2) = '2'
  | (Builtins.equalsInteger i 3) = '3'
  | (Builtins.equalsInteger i 4) = '4'
  | (Builtins.equalsInteger i 5) = '5'
  | (Builtins.equalsInteger i 6) = '6'
  | (Builtins.equalsInteger i 7) = '7'
  | (Builtins.equalsInteger i 8) = '8'
  | (Builtins.equalsInteger i 9) = '9'
  | otherwise = '0' -- Fix this

</code></pre>
<p>Then instead of <code>checkValidMint</code>, you can just check that the expected amount of your new token is included in the output value in the policy <code>ctx</code>, like <a href="https://github.com/input-output-hk/plutus-apps/blob/c4cc9ee1add8f421e57789befb6a0c90621801f1/plutus-use-cases/src/Plutus/Contracts/Uniswap/OnChain.hs#L105" rel="nofollow noreferrer">here</a> in the Uniswap example.</p>

CC BY-SA 4.0
2022-05-30T18:16:04.460Z
Unfortunately, I cannot get this code to run. It seems like `charToString` is not part of the API anymore. And String has been replaced with `BuiltinString`.

I am not sure how to make use of this code. Any other suggestions?
Will
CC BY-SA 4.0
2022-05-30T22:25:51.443Z
Make sure you are using `INLINEABLE` on your code, try using `equalsInteger` instead of `==`, and see where that lands you. You might need to dig and see what methods on `char` there are instead of `charToString`. I'd need to do some digging to say for sure what all has changed.
Mitchell Turner
CC BY-SA 4.0
2022-05-26T21:13:45.467Z
2022-05-26T21:13:45.467Z
Mitchell Turner
<p>You may want to checkout the utils package I used for the open source cardano lottery located here: <a href="https://github.com/lley154/cardano-lottery/blob/main/src/Utils.hs" rel="nofollow noreferrer">https://github.com/lley154/cardano-lottery/blob/main/src/Utils.hs</a>.   This code has been tested and works on the testnet.</p>
<p>Also, there was a similar post that may help if you having issues and need to be aware of the encoding of builtinbytestrings in plutus.  <a href="https://cardano.stackexchange.com/questions/7440/how-to-get-a-base-16-hex-builtinstring-from-gettxid/7805#7805">How to get a base 16 hex BuiltinString from getTxId</a></p>

CC BY-SA 4.0
2022-05-31T13:44:19.023Z
2022-05-31T13:44:19.023Z
lley154
<p>After testing out many different variations, changing imports to make sure the correct functions of <code>Prelude</code> and <code>PlutusTx.Prelude</code> are used, I've found the problem:</p>
<ol>
<li>I made sure this pragma is present: <code>{-# LANGUAGE NoImplicitPrelude #-}</code></li>
<li>I imported both Preludes in a qualified way:</li>
</ol>
<pre><code>import           PlutusTx.Prelude as PP
import           Prelude          as P
</code></pre>
<p>which let's me clearly distinguish what operators to use when it comes to <code>Integer</code> comparisons. I subsequently replaces all <code>==</code> with either <code>PP.==</code> for <strong>on-chain</strong> and <code>P.==</code> for <strong>off-chain</strong> code.</p>
<ol start="3">
<li>Lastly, I found one more error where I used Integer pattern matching <strong>on-chain</strong> like so:</li>
</ol>
<pre><code>checkMint :: String -&gt; Integer -&gt; Bool
checkMint s (1)  = ...
checkMint s (-1) = ...
checkMint _ _    = False
</code></pre>
<p>The pattern matching seems to default to <code>P.==</code> for this <strong>on-chain</strong> code, so I replace it with an <em>if-then-else</em> and it worked fine.</p>
<pre><code>checkMint s amt = 
    if amt PP.== 1 then ... 
    else if amt PP.== -1 then ... 
    else ...
</code></pre>

CC BY-SA 4.0
2022-06-06T08:52:37.157Z
2022-06-06T08:52:37.157Z
Will
<p>I want to check <strong>on-chain</strong> if a given <code>TokenName</code> is of a specific pattern, for example:</p>
<ul>
<li><code>aaa1</code></li>
<li><code>aaa2</code></li>
<li><code>aaa3</code></li>
</ul>
<p>I have something like this, but it throws an interpreter exception:</p>
<pre><code>checkValidMint :: TokenName -&gt; Integer -&gt; Bool
checkValidMint tn count  = tn == (TokenName $ &quot;aaa&quot; &lt;&gt; (integerToByteString count))

integerToByteString :: Integer -&gt; BuiltinByteString
integerToByteString n
    | n == 0 = &quot;0&quot;
    | n == 1 = &quot;1&quot;
    | n == 2 = &quot;2&quot;
    | n == 3 = &quot;3&quot;
    | n == 4 = &quot;4&quot;
    | n == 5 = &quot;5&quot;
    | n == 6 = &quot;6&quot;
    | n == 7 = &quot;7&quot;
    | n == 8 = &quot;8&quot;
    | n == 9 = &quot;9&quot;
    | otherwise = integerToByteString (n `P.divide` 10) P.&lt;&gt; integerToByteString (n `P.modulo` 10)
</code></pre>
<p><strong>Update 2</strong>
Also tried:</p>
<pre><code>integerToByteString n
            | (Builtins.equalsInteger n 0) = &quot;0&quot;
            | (Builtins.equalsInteger n 1) = &quot;1&quot;
            | (Builtins.equalsInteger n 2) = &quot;2&quot;
            | (Builtins.equalsInteger n 3) = &quot;3&quot;
            | (Builtins.equalsInteger n 4) = &quot;4&quot;
            | (Builtins.equalsInteger n 5) = &quot;5&quot;
            | (Builtins.equalsInteger n 6) = &quot;6&quot;
            | (Builtins.equalsInteger n 7) = &quot;7&quot;
            | (Builtins.equalsInteger n 8) = &quot;8&quot;
            | (Builtins.equalsInteger n 9) = &quot;9&quot;
            | otherwise = integerToByteString (n `PlutusTx.Prelude.divide` 10) PP.&lt;&gt; integerToByteString (n `PlutusTx.Prelude.modulo` 10)
</code></pre>
<p>I keep getting an interpreter error:</p>
<pre><code>GHC Core to PLC plugin: E042:Error: Unsupported feature: Use of Haskell Integer equality, possibly via the Haskell Eq typeclass Context: Compiling expr: GHC.Integer.Type.eqInteger# Context: Compiling expr: GHC.Integer.Type.eqInteger# x Context: Compiling expr: GHC.Integer.Type.eqInteger# x y Context: Compiling expr: case GHC.Integer.Type.eqInteger# x y of wild [Occ=Once] { __DEFAULT -&gt; GHC.Prim.tagToEnum# @ GHC.Types.Bool wild } Context: Compiling expr: \ (y [Occ=Once] :: GHC.Integer.Type.Integer) -&gt; case GHC.Integer.Type.eqInteger# x y of ...
</code></pre>
<pre><code></code></pre>

<plutus><on-chain>
How to compare BuiltinByteStrings onchain?
# How to compare BuiltinByteStrings onchain?

(Asked by: username_0 on 2022-05-26)

I want to check on-chain if a given TokenName is of a specific pattern, for example:

aaa1
aaa2
aaa3

I have something like this, but it throws an interpreter exception:
checkValidMint :: TokenName -> Integer -> Bool
checkValidMint tn count  = tn == (TokenName $ "aaa" <> (integerToByteString count))

integerToByteString :: Integer -> BuiltinByteString
integerToByteString n
    | n == 0 = "0"
    | n == 1 = "1"
    | n == 2 = "2"
    | n == 3 = "3"
    | n == 4 = "4"
    | n == 5 = "5"
    | n == 6 = "6"
    | n == 7 = "7"
    | n == 8 = "8"
    | n == 9 = "9"
    | otherwise = integerToByteString (n `P.divide` 10) P.<> integerToByteString (n `P.modulo` 10)

Update 2
Also tried:
integerToByteString n
            | (Builtins.equalsInteger n 0) = "0"
            | (Builtins.equalsInteger n 1) = "1"
            | (Builtins.equalsInteger n 2) = "2"
            | (Builtins.equalsInteger n 3) = "3"
            | (Builtins.equalsInteger n 4) = "4"
            | (Builtins.equalsInteger n 5) = "5"
            | (Builtins.equalsInteger n 6) = "6"
            | (Builtins.equalsInteger n 7) = "7"
            | (Builtins.equalsInteger n 8) = "8"
            | (Builtins.equalsInteger n 9) = "9"
            | otherwise = integerToByteString (n `PlutusTx.Prelude.divide` 10) PP.<> integerToByteString (n `PlutusTx.Prelude.modulo` 10)

I keep getting an interpreter error:
GHC Core to PLC plugin: E042:Error: Unsupported feature: Use of Haskell Integer equality, possibly via the Haskell Eq typeclass Context: Compiling expr: GHC.Integer.Type.eqInteger# Context: Compiling expr: GHC.Integer.Type.eqInteger# x Context: Compiling expr: GHC.Integer.Type.eqInteger# x y Context: Compiling expr: case GHC.Integer.Type.eqInteger# x y of wild [Occ=Once] { __DEFAULT -> GHC.Prim.tagToEnum# @ GHC.Types.Bool wild } Context: Compiling expr: \ (y [Occ=Once] :: GHC.Integer.Type.Integer) -> case GHC.Integer.Type.eqInteger# x y of ...

**Tags:** plutus, on-chain

---

## 3 answers

### Answer by username_1 on 2022-05-26. Score: 1 | Accepted answer

This is how I've done the integer conversion in the past. Might need some minor updating:
{-# INLINEABLE intToString #-}
intToString :: Integer -> Builtins.String
intToString i = Prelude.foldr Builtins.appendString "" strings
  where
    ints = intToInts i
    strings = map (Builtins.charToString . intToChar) ints


{-# INLINEABLE intToChar #-}
intToChar :: Integer -> Haskell.Char
intToChar i
  | (Builtins.equalsInteger i 0) = '0'
  | (Builtins.equalsInteger i 1) = '1'
  | (Builtins.equalsInteger i 2) = '2'
  | (Builtins.equalsInteger i 3) = '3'
  | (Builtins.equalsInteger i 4) = '4'
  | (Builtins.equalsInteger i 5) = '5'
  | (Builtins.equalsInteger i 6) = '6'
  | (Builtins.equalsInteger i 7) = '7'
  | (Builtins.equalsInteger i 8) = '8'
  | (Builtins.equalsInteger i 9) = '9'
  | otherwise = '0' -- Fix this


Then instead of checkValidMint, you can just check that the expected amount of your new token is included in the output value in the policy ctx, like here in the Uniswap example.

> **Comments:**
> **username_0**: Unfortunately, I cannot get this code to run. It seems like `charToString` is not part of the API anymore. And String has been replaced with `BuiltinString`.  I am not sure how to make use of this code. Any other suggestions?  
> **username_1**: Make sure you are using `INLINEABLE` on your code, try using `equalsInteger` instead of `==`, and see where that lands you. You might need to dig and see what methods on `char` there are instead of `charToString`. I'd need to do some digging to say for sure what all has changed.  

---

### Answer by username_2 on 2022-05-31. Score: 0

You may want to checkout the utils package I used for the open source cardano lottery located here: <https://github.com/lley154/cardano-lottery/blob/main/src/Utils.hs>. This code has been tested and works on the testnet.

Also, there was a similar post that may help if you having issues and need to be aware of the encoding of builtinbytestrings in plutus. [How to get a base 16 hex BuiltinString from getTxId](https://cardano.stackexchange.com/questions/7440/how-to-get-a-base-16-hex-builtinstring-from-gettxid/7805#7805)


---

### Answer by username_0 on 2022-06-06. Score: 0

After testing out many different variations, changing imports to make sure the correct functions of Prelude and PlutusTx.Prelude are used, I've found the problem:

I made sure this pragma is present: {-# LANGUAGE NoImplicitPrelude #-}
I imported both Preludes in a qualified way:

import           PlutusTx.Prelude as PP
import           Prelude          as P

which let's me clearly distinguish what operators to use when it comes to Integer comparisons. I subsequently replaces all == with either PP.== for on-chain and P.== for off-chain code.

Lastly, I found one more error where I used Integer pattern matching on-chain like so:

checkMint :: String -> Integer -> Bool
checkMint s (1)  = ...
checkMint s (-1) = ...
checkMint _ _    = False

The pattern matching seems to default to P.== for this on-chain code, so I replace it with an if-then-else and it worked fine.
checkMint s amt = 
    if amt PP.== 1 then ... 
    else if amt PP.== -1 then ... 
    else ...


---

