
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
Convert Data to Hexadecimal in Haskell
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 () main = putStrLn $ stringToHex "hello world 123"
Output
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... 68656c6c6f20776f726c642031323
Method 2: Converting Data to Hexadecimal using showHex function
In this method, the convertToHex function takes a value of any Integral type and converts it to a hexadecimal string using the showHex function. The second argument of showHex is a string that is appended to the end of the hexadecimal representation, which we leave empty in this case.
Algorithm
Step 1 ? The Numeric module is imported.
-
Step 2 ? The convertToHex function is defined using showHex function as,
convertToHex x = showHex x "". 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 convertToHex function is called with some value.
Step 5 ? 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 showHex function.
import Numeric (showHex) convertToHex :: (Integral a, Show a) => a -> String convertToHex x = showHex x "" main :: IO () main = do let value = 255 hex = convertToHex value putStrLn hex
Output
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... ff
Example 2
In this example, Data is converted to Hexadecimal using showHex function.
import Numeric dataToHex :: (Integral a, Show a) => a -> String dataToHex n = "0x" ++ showHex n "" main :: IO () main = do let num = 255 :: Int putStrLn $ dataToHex num
Output
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... 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
n = fromEnum c
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 n = fromEnum c 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 ) Linking main ... 68656C6C6F20776F726C6420313233
Conclusion
In Haskell, data to hexadecimal conversion refers to the process of converting a given data or value into a hexadecimal string. Hexadecimal representation is a commonly used format to represent binary data or memory addresses, as it is easy to read and write. There are various ways to perform this conversion in Haskell, depending on the desired implementation and requirements.