I am trying to generate a random lazy ByteString of the same length as a lazy ByteString that I already have.
So I take the length of the ByteString and feed it into getEntropy like so:
import qualified Data.ByteString.Lazy.Char8 as L
import qualified System.Entropy as SE
string :: L.ByteString
string = L.pack "Hello world!"
randomString :: IO L.ByteString
randomString = L.fromChunks . (:[]) <$> SE.getEntropy (L.length string)
(using L.fromChunks . (:[]) to convert from a strict ByteString to a lazy one.)
The problem is that SE.getEntropy is of type Int -> IO ByteString while L.length is of type L.ByteString -> GHC.Int.Int64.
How can I convert an Int64 to an Int?
You can turn any Integral type into another Num type using fromIntegral:
fromInt64ToInt :: Int64 -> Int
fromInt64ToInt = fromIntegral
fromIntegral
In GHCI:
let a = 6 :: GHC.Int.Int64
let f :: Int -> Int; f x = x;
--this will error
f a
--this succeeds
f (fromIntegral a)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With