Reputation: 51
I am working on implementing the RSA encryption system for a class or mine and to be able to implement it we obviously would need to convert strings to integers and then the reverse of that. Using basic level haskell can someone explain to me how this might be done. I have a function that will tell me the max length that a character code can be for your consideration as well.
Thank you everyone.
Upvotes: 0
Views: 3332
Reputation: 1918
You may also use the ByteString
module: convert a simple String
into a ByteString
with pack
and then directly work with it as an array of bytes (Word8
's, if to be exact).
Upvotes: 0
Reputation: 6543
I am not sure exactly what you need, but if you are just trying to get a numerical representation of a String
, you can use Data.Char.ord
which will convert a Char
to an Int
. Then you can use Data.Char.chr
to convert each Int
back to a Char
.
Using:
import Data.Char (ord, chr)
stringsToInts :: String -> [Int]
stringToInts = map ord
intsToString :: [Int] -> String
intsToString = map chr
you can:
Prelude Data.Char> let ints = stringsToInts "Hello world!"
Prelude Data.Char> ints
[72,101,108,108,111,32,119,111,114,108,100,33]
Prelude Data.Char> intsToString ints
"Hello world!"
You could then further process the list ints
if that is what you need (and further modify intsToString
.)
Upvotes: 4