In Haskell, we will convert Data to Hexadecimal by using intToDigit, toHex and showHex functions. In the first example, we are going to use toHex function and (stringToHex = concatMap (\c -> toHex (fromEnum c))) function and in the second example, we are going to use convertToHex x = showHex x ""). And in the third example, we are going to use (toHex = concatMap toHexChar).

## Method 1: Converting Data to Hexadecimal using intToDigit and toHex function

In this method, the toHex function converts an integer to its hexadecimal representation by repeatedly dividing by 16 and converting each remainder to a digit using intToDigit. The result is reversed so that the least significant digit comes first. The stringToHex function uses fromEnum to convert each character in the input string to an integer, and then calls toHex on each integer to obtain its hexadecimal representation. The resulting hexadecimal digits are concatenated to form the final output string.

### Algorithm

• Step 1 − The Data.Char module is imported.

• Step 2 − The toHex function is defined as,

toHex x = reverse (toHex' x)

where

toHex' 0 = "0"

toHex' n = let (q, r) = n divMod 16

in if q == 0

then [intToDigit r]

else intToDigit r : toHex' q.

The stringToHex function is defined as,

stringToHex = concatMap (\c -> toHex (fromEnum c)).

• 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 stringToHex function is called with some data.

• Step 5 − The resultant hexadecimal value corresponding to the data is printed to the console.

### Example 1

In this example, Data is converted to Hexadecimal using intToDigit and toHex function.

import Data.Char (intToDigit)

toHex :: Int -> String
toHex x = reverse (toHex' x)
where
toHex' 0 = "0"
toHex' n = let (q, r) = n divMod 16
in if q == 0
then [intToDigit r]
else intToDigit r : toHex' q

stringToHex :: String -> String
stringToHex = concatMap (\c -> toHex (fromEnum c))

main :: IO ()


### Output

[1 of 1] Compiling Main             ( main.hs, main.o )
0xff


## Method 3: Converting Data to Hexadecimal using concatMap and toEnum function

In this method, the toHex function takes a string as input and applies the toHexChar function to each character in the string using concatMap. The toHexChar function converts a single character to its hexadecimal representation by computing the integer value of the character, and then computing the hexadecimal digits for each nibble (4-bit) of the integer. The resulting hexadecimal digits are concatenated into a string and returned.

### Algorithm

• Step 1 − The toHex function is defined as,

toHex = concatMap toHexChar

where

toHexChar c =

let

hexDigit x

| x < 10 = toEnum (x + 48)

| otherwise = toEnum (x + 55)

in [hexDigit (n div 16), hexDigit (n mod 16)].

• 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 toHex function is called with some value.

• Step 4 − The resultant hexadecimal value corresponding to the data value is printed to the console.

### Example 1

In this example, Data is converted to Hexadecimal using concatMap and toEnum function.

toHex :: String -> String
toHex = concatMap toHexChar
where
toHexChar c =
let
hexDigit x
| x < 10    = toEnum (x + 48)
| otherwise = toEnum (x + 55)
in [hexDigit (n div 16), hexDigit (n mod 16)]
main :: IO ()
main = putStrLn \$ toHex "hello world 123"


### Output

[1 of 1] Compiling Main             ( main.hs, main.o )