user2537: 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.

user934: 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


user1130: 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.

2537: <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>
 934: <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>
 1130: <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>
