Haskell Program to Convert File to Byte Array


In Haskell, we will convert File to byte array by using B.readFile function along with fromIntegral and foldl function. In the first example, we are going to use ( let byteArray = B.unpack bytes) and in the second example, we are going to use (let wordArray = map fromIntegral (B.unpack bytes) :: [Word8]). And in the third example, we are going to use (let byteArray = B.foldl' (\acc byte -> acc ++ [byte]) [] bytes).

Algorithm

  • Step 1 − The Data.ByteString modules are imported.

  • Step 2 − 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 3 − The B.readFile function is called with the file name.

  • Step 4 − The byte array named, “byteArray” is being initialized to unpack the bytes of the file.

  • Step 5 − The resultant content of the text file is printed to the console in the form of byte array using print function.

Example 1

In this example, we import the Data.ByteString and Data.ByteString.Char8 modules, which provide functions for working with byte strings. We then use the B.readFile function to read the contents of a file called filename.txt into a ByteString value named bytes. We then use the B.unpack function to convert the ByteString value to a list of bytes (i.e., a byte array) named byteArray. Finally, we print the byte array using the print function.

import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C

main :: IO ()
main = do
   bytes <- B.readFile "filename.txt"
   let byteArray = B.unpack bytes
   print byteArray

Output

Hello, world!

Example 2

In this example, the file is converted to byte array using B.readFile function. But instead of printing the entire byte array at once, it prints each byte on a separate line using the mapM_ function.

import qualified Data.ByteString as B

main :: IO ()
main = do
   bytes <- B.readFile "filename.txt"
   let byteArray = B.unpack bytes
   mapM_ print byteArray

Output

Hello, world!

Example 3

In this example, we use the fromIntegral function to convert each byte from an Int to a Word8. We then use the :: operator to specify that the resulting list should have type [Word8]. This can be useful if you need to perform arithmetic or bitwise operations on the bytes.

import qualified Data.ByteString as B
import Data.Word (Word8)

main :: IO ()
main = do
   bytes <- B.readFile "filename.txt"
   let wordArray = map fromIntegral (B.unpack bytes) :: [Word8]
   mapM_ print wordArray

Output

Hello, world!

Example 4

In this method, we use the B.foldl' function to apply a function that accumulates the bytes into a list. The initial accumulator value is the empty list [], and the accumulating function simply appends each byte to the end of the list. This approach can be slower than the previous ones, but it can be useful if you need to perform more complex operations on the bytes while reading the file.

import qualified Data.ByteString as B

main :: IO ()
main = do
   bytes <- B.readFile "filename.txt"
   let byteArray = B.foldl' (\acc byte -> acc ++ [byte]) [] bytes
   mapM_ print byteArray

Output

Hello, world!

Conclusion

In Haskell, a file can be read as a stream of bytes and then converted into a byte array. A byte array is a sequence of bytes that can be used to represent binary data. It can be useful for storing or transmitting binary data, such as images, audio, or video files. To convert a file to a byte array in Haskell, we can use the Data.ByteString module, which provides efficient functions for working with byte strings. Specifically, we can use the B.readFile function to read the contents of a file into a ByteString value, and then use the B.unpack function to convert the ByteString value to a list of bytes (i.e., a byte array).

Updated on: 25-Apr-2023

213 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements