<p>I had trouble with the same thing, just going the opposite direction, <code>Slot -&gt; TokenName</code> (<code>Slot</code> is just an <code>Integer</code> under the hood). After a bunch of pain I ended up just coding my own parsing logic:</p>
<pre><code>-- Simplified business code here:
{-# INLINEABLE dcForSlot #-}
tokenNameForSlot :: Slot -&gt; TokenName
tokenNameForSlot slot = TokenName $ Builtins.encodeUtf8 $ intToString $ getSlot slot

{-# 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 intToInts #-}
intToInts :: Integer -&gt; [Integer]
intToInts i
  | (Builtins.equalsInteger a 0) = [b]
  | otherwise = (intToInts a) ++ [b]
  where
    (a, b) = divMod i 10

{-# 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>Feel free to try and reverse this to get what you want. It might be easier going the other direction, I don't have time rn to figure out what it would look like but I feel your pain, so I thought I'd share.</p>

CC BY-SA 4.0
2021-09-22T19:55:33.043Z
What does 'intToInts' do ?
Jey
CC BY-SA 4.0
2021-09-22T20:13:36.880Z
It splits all the digits of the integer:
`intToInts 5701 == [5, 7, 0, 1]`
Mitchell Turner
CC BY-SA 4.0
2021-09-23T06:38:29.220Z
Thank you, I understand. I will post my solution as soon as I have it. It's crazy that we have to use such work arounds on Plutus.
Jey
CC BY-SA 4.0
2021-09-23T06:43:10.450Z
Yeah. It's young still. There's plenty of room for people to add libraries. There's a big chance I missed some tools as well.
Mitchell Turner
CC BY-SA 4.0
2021-09-21T20:47:14.833Z
2021-09-21T20:47:14.833Z
Mitchell Turner
<p>This is slow but works.</p>
<pre><code>{-# LANGUAGE  OverloadedStrings  #-}

module StackExchange where

import Prelude
import PlutusTx.Builtins.Class
import PlutusTx.Builtins.Internal
import Data.ByteString hiding (map)
import Data.Char

s = stringToBuiltinByteString &quot;123&quot;

bsToMaybeInteger :: BuiltinByteString -&gt; Maybe Integer
bsToMaybeInteger str
  | Prelude.all (\x -&gt; x &gt;= 48 &amp;&amp; x &lt;= 57) unpackedString = Just (Prelude.read (Prelude.map chr unpackedString) :: Integer)
  | otherwise                                             = Nothing
  where
    unpackedString = map fromEnum . unpack . fromBuiltin $ str
</code></pre>
<p>Can be tested in a <code>cabal repl</code> session like this:</p>
<pre><code>λ&gt; StackExchange.bsToMaybeInteger StackExchange.s
Just 123
</code></pre>

CC BY-SA 4.0
2021-09-21T21:40:15.017Z
Have you tried this in compiled on-chain code?
Mitchell Turner
CC BY-SA 4.0
2021-09-21T21:46:21.277Z
fair point, I have not done that yet... I guess there may be some slight modifications necessary.
Matthias Sieber
CC BY-SA 4.0
2021-09-21T22:09:20.427Z
Yeah. That's where I had the biggest headache, especially since the Plutus compiler doesn't give very helpful error messages.
Mitchell Turner
CC BY-SA 4.0
2021-09-21T21:36:24.887Z
2021-09-21T21:36:24.887Z
Matthias Sieber
<p>I have information encoded in the <code>TokenName</code> of an asset. These token names represent integers.</p>
<p>According to the Plutus source code, the <code>TokenName</code> is of type <code>BuiltinByteString</code>. How can I parse it to type Integer in the on chain code ? In short I am looking for a Plutus-pendant of the Haskell <code>read</code>, but one that returns a <code>Maybe Integer</code>.</p>

<plutus><on-chain>
How to parse TokenName to Integer
# How to parse TokenName to Integer

(Asked by: username_0 on 2021-09-21)

I have information encoded in the `TokenName` of an asset. These token names represent integers.

According to the Plutus source code, the `TokenName` is of type `BuiltinByteString`. How can I parse it to type Integer in the on chain code ? In short I am looking for a Plutus-pendant of the Haskell `read`, but one that returns a `Maybe Integer`.

**Tags:** plutus, on-chain

---

## 2 answers

### Answer by username_1 on 2021-09-21. Score: 2

I had trouble with the same thing, just going the opposite direction, Slot -> TokenName (Slot is just an Integer under the hood). After a bunch of pain I ended up just coding my own parsing logic:
-- Simplified business code here:
{-# INLINEABLE dcForSlot #-}
tokenNameForSlot :: Slot -> TokenName
tokenNameForSlot slot = TokenName $ Builtins.encodeUtf8 $ intToString $ getSlot slot

{-# INLINEABLE intToString #-}
intToString :: Integer -> Builtins.String
intToString i = Prelude.foldr Builtins.appendString "" strings
  where
    ints = intToInts i
    strings = map (Builtins.charToString . intToChar) ints

{-# INLINEABLE intToInts #-}
intToInts :: Integer -> [Integer]
intToInts i
  | (Builtins.equalsInteger a 0) = [b]
  | otherwise = (intToInts a) ++ [b]
  where
    (a, b) = divMod i 10

{-# 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

Feel free to try and reverse this to get what you want. It might be easier going the other direction, I don't have time rn to figure out what it would look like but I feel your pain, so I thought I'd share.

> **Comments:**
> **username_0**: What does 'intToInts' do ?  
> **username_1**: It splits all the digits of the integer: `intToInts 5701 == [5, 7, 0, 1]`  
> **username_0**: Thank you, I understand. I will post my solution as soon as I have it. It's crazy that we have to use such work arounds on Plutus.  
> **username_1**: Yeah. It's young still. There's plenty of room for people to add libraries. There's a big chance I missed some tools as well.  

---

### Answer by username_2 on 2021-09-21. Score: 1

This is slow but works.
{-# LANGUAGE  OverloadedStrings  #-}

module StackExchange where

import Prelude
import PlutusTx.Builtins.Class
import PlutusTx.Builtins.Internal
import Data.ByteString hiding (map)
import Data.Char

s = stringToBuiltinByteString "123"

bsToMaybeInteger :: BuiltinByteString -> Maybe Integer
bsToMaybeInteger str
  | Prelude.all (\x -> x >= 48 && x <= 57) unpackedString = Just (Prelude.read (Prelude.map chr unpackedString) :: Integer)
  | otherwise                                             = Nothing
  where
    unpackedString = map fromEnum . unpack . fromBuiltin $ str

Can be tested in a cabal repl session like this:
λ> StackExchange.bsToMaybeInteger StackExchange.s
Just 123

> **Comments:**
> **username_1**: Have you tried this in compiled on-chain code?  
> **username_2**: fair point, I have not done that yet... I guess there may be some slight modifications necessary.  
> **username_1**: Yeah. That's where I had the biggest headache, especially since the Plutus compiler doesn't give very helpful error messages.  

---

