Skip to main content
added 68 characters in body
Source Link
raghu
  • 1.8k
  • 7
  • 24

Token name is a Bytestring therefore it has to handle like that. I am learning and my attempt at extracting the integer value is like this:-

import Data.Char ( ord )
import Data.ByteString.Char8 (ByteString, unpack,pack)

getIdFromTokenName :: ByteString -> Integer
getIdFromTokenName tn = numbers (reverse (unpack tn)) 0 1

numbers :: String->Integer->Integer -> Integer 
numbers [] x y = x
numbers (l:ls) x y = if ord l >= 48 && ord l <= 57
    then numbers ls ((toInteger (ord l) - 48)* y + x) (y*10)
    else numbers ls x y
import Data.Char ( ord )
import Data.ByteString.Char8 (ByteString, unpack,pack)

getIdFromTokenName :: ByteString -> Integer
getIdFromTokenName tn = numbers (reverse (unpack tn)) 0 1

numbers :: String->Integer->Integer -> Integer 
numbers [] x y = x
numbers (l:ls) x y = if ord l >= 48 && ord l <= 57
    then numbers ls ((toInteger (ord l) - 48)* y + x) (y*10)
    else numbers ls x y

I used pack unpacked from the module imported above in ghci it was working like this

*Main> getIdFromTokenName (pack "NUM123") 
123

This is not a safe function to extract numbers and is also not the best method I feel. It's not checked in the Plutus setting also.

Token name is a Bytestring therefore it has to handle like that. I am learning and my attempt at extracting the integer value is like this:-

import Data.Char ( ord )
import Data.ByteString.Char8 (ByteString, unpack,pack)

getIdFromTokenName :: ByteString -> Integer
getIdFromTokenName tn = numbers (reverse (unpack tn)) 0 1

numbers :: String->Integer->Integer -> Integer 
numbers [] x y = x
numbers (l:ls) x y = if ord l >= 48 && ord l <= 57
    then numbers ls ((toInteger (ord l) - 48)* y + x) (y*10)
    else numbers ls x y

I used pack unpacked from the module imported above in ghci it was working like this

*Main> getIdFromTokenName (pack "NUM123") 
123

This is not a safe function to extract numbers and is also not the best method I feel. It's not checked in the Plutus setting also.

Token name is a Bytestring therefore it has to handle like that. I am learning and my attempt at extracting the integer value is like this:-

import Data.Char ( ord )
import Data.ByteString.Char8 (ByteString, unpack,pack)

getIdFromTokenName :: ByteString -> Integer
getIdFromTokenName tn = numbers (reverse (unpack tn)) 0 1

numbers :: String->Integer->Integer -> Integer 
numbers [] x y = x
numbers (l:ls) x y = if ord l >= 48 && ord l <= 57
    then numbers ls ((toInteger (ord l) - 48)* y + x) (y*10)
    else numbers ls x y

I used pack unpacked from the module imported above in ghci it was working like this

*Main> getIdFromTokenName (pack "NUM123") 
123

This is not a safe function to extract numbers and is also not the best method I feel. It's not checked in the Plutus setting also.

Source Link
raghu
  • 1.8k
  • 7
  • 24

Token name is a Bytestring therefore it has to handle like that. I am learning and my attempt at extracting the integer value is like this:-

import Data.Char ( ord )
import Data.ByteString.Char8 (ByteString, unpack,pack)

getIdFromTokenName :: ByteString -> Integer
getIdFromTokenName tn = numbers (reverse (unpack tn)) 0 1

numbers :: String->Integer->Integer -> Integer 
numbers [] x y = x
numbers (l:ls) x y = if ord l >= 48 && ord l <= 57
    then numbers ls ((toInteger (ord l) - 48)* y + x) (y*10)
    else numbers ls x y

I used pack unpacked from the module imported above in ghci it was working like this

*Main> getIdFromTokenName (pack "NUM123") 
123

This is not a safe function to extract numbers and is also not the best method I feel. It's not checked in the Plutus setting also.