Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Haskell Program to Convert String value to byte Value
In Haskell, we will convert String Value to Byte value by using ord , B.pack and fromNum functions. In the first example, we are going to use (stringToBytes = map ord ) function and in the second example, we are going to call (stringToBytes = B.pack) function. And in the third example, we are going to use (byteList = map fromEnum str) function.
Algorithm
Step 1 ? The Data.Char module is imported.
Step 2 ? The stringToBytes function is defined using ord function as,
Step 3 ? The program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.
Step 4 ? The variable named, ?str' is defined that will hold the String that is to be converted to byte.
Step 5 ? The resultant byte value corresponding to the string value is printed to the console, once the function is being called.
Example 1
In this example, string value is converted to byte value using ord function.
import Data.Char (ord)
stringToBytes :: String -> [Int]
stringToBytes = map ord
main :: IO ()
main = do
let str = "hello"
bytes = stringToBytes str
print bytes
Output
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... [104,101,108,108,111]
Example 2
In this example, string value is converted to byte value using B.pack function.
import qualified Data.ByteString.Char8 as B
stringToBytes :: String -> B.ByteString
stringToBytes = B.pack
main :: IO ()
main = do
let str = "hello"
bytes = stringToBytes str
B.putStrLn bytes
Output
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... hello
Example 3
In this example, string value is converted to byte value using B.pack function.
import qualified Data.ByteString.Char8 as B
main :: IO ()
main = do
let str = "hello world"
byteStr = B.pack str
putStrLn $ "String: " ++ str
putStrLn $ "ByteString: " ++ show byteStr
Output
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... String: hello world ByteString: "hello world"
Example 4
In this example, string value is converted to byte value using B.pack function.
import qualified Data.ByteString.Char8 as B
main :: IO ()
main = do
let str = "hello world"
byteStr = B.pack str
byteList = B.unpack byteStr
putStrLn $ "String: " ++ str
putStrLn $ "ByteList: " ++ show byteList
Output
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... String: hello world ByteList: "hello world"
Example 5
In this example, string value is converted to byte value using fromEnum function.
import Data.List (map)
main :: IO ()
main = do
let str = "hello world"
byteList = map fromEnum str
putStrLn $ "String: " ++ str
putStrLn $ "ByteList: " ++ show byteList
Output
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... String: hello world ByteList: [104,101,108,108,111,32,119,111,114,108,100]
Conclusion
String value to byte value conversion in Haskell is the process of converting a sequence of characters represented as a string into a sequence of bytes. In Haskell, strings are represented as sequences of characters, while bytes are represented as sequences of numeric values in the range 0-255 that represent the values of individual bytes.