Haskell Program to convert Decimal to Hexadecimal


In Haskell, we can use intToDigits, showIntAtBase and format functions to convert a decimal to Hexadecimal number. In the first example, we are going to use (decToHex n = reverse (hexChars n) where hexChars 0 = "" and hexChars x = intToDigit (x `mod` 16) : hexChars (x `div` 16)) and in the second example, we are going to use (decToHex n = showIntAtBase 16 intToDigit n "") function. In the third example, we are going to use (decToHex n = printf "%X" n) function.

Method 1: Using reverse and intToDigits functions

In this method, the Data.Char module is imported to use the intToDigit function which converts an integer to its corresponding hexadecimal character.

The decToHex function recursively converts the input integer to hexadecimal by repeatedly taking the remainder and quotient of the input integer divided by 16, and appending the corresponding hexadecimal character to a list. The resulting list is then reversed to get the correct order of the hexadecimal characters.

Algorithm

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

  • Step 2 − The decToHex function is defined using reverse and intToDigit function as,

decToHex 0 = "0"
decToHex n = reverse (hexChars n)
   where
      hexChars 0 = ""
      hexChars x = intToDigit (x `mod` 16) : hexChars (x `div` 16).
  • Step 3 − 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, “n” is being initialized. It will hold the decimal number that is to be converted to respective hexadecimal number.

  • Step 5 − The resultant hexadecimal number is printed to the console using ‘putStrLn’ statement after the function is called.

Example

In this example, the recursive function is defined using intToDigit and reverse functions to convert the decimal to Hexadecimal number.

import Data.Char (intToDigit)

decToHex :: Int -> String
decToHex 0 = "0"
decToHex n = reverse (hexChars n)
   where
      hexChars 0 = ""
      hexChars x = intToDigit (x `mod` 16) : hexChars (x `div` 16)

main :: IO ()
main = do
   let n = 52
   putStrLn $ "The hexadecimal representation of " ++ show n ++ " is " ++ decToHex n

Output

The hexadecimal representation of 52 is 34

Method 2: Using showIntAtBase functions

In this method, the Numeric and Data.Char modules are imported to use the showIntAtBase and intToDigit functions, respectively. The decToHex function calls showIntAtBase with a base of 16 and intToDigit as the conversion function to get the hexadecimal string representation of the input integer.

Algorithm

  • Step 1 − The Data.Char and Numeric libraries are imported.

  • Step 2 − The decToHex function is defined using showIntAtBase function as,

decToHex n = showIntAtBase 16 intToDigit n "".
  • Step 3 − 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, “n” is being initialized. It will hold the decimal number that is to be converted to respective hexadecimal number.

  • Step 5 − The resultant hexadecimal number is printed to the console using ‘putStrLn’ statement after the function is called.

Example

In this example, the function is defined using intToDigit and showIntAtBase functions to convert the decimal to Hexadecimal number.

import Numeric (showIntAtBase)
import Data.Char (intToDigit)

decToHex :: Int -> String
decToHex n = showIntAtBase 16 intToDigit n ""

main :: IO ()
main = do
   let n = 52
   putStrLn $ "The hexadecimal representation of " ++ show n ++ " is " ++ decToHex n

Output

The hexadecimal representation of 52 is 34

Method 3: Using format function

In this method, the Text.Printf module is imported to use the printf function, which can format an integer as a hexadecimal string using the %X format specifier. The decToHex function simply calls printf with the %X format specifier and the input integer to get the hexadecimal string representation.

Algorithm

  • Step 1 − The Text.Printf library is imported.

  • Step 2 − The decToHex function is defined using format function as,

decToHex n = printf "%X" n.
  • Step 3 − 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, “n” is being initialized. It will hold the decimal number that is to be converted to respective hexadecimal number.

  • Step 5 − The resultant hexadecimal number is printed to the console using ‘putStrLn’ statement after the function is called.

Example

In this example, the function is defined using format function to convert the decimal to Hexadecimal number.

import Text.Printf (printf)

decToHex :: Int -> String
decToHex n = printf "%X" n

main :: IO ()
main = do
   let n = 52
   putStrLn $ "The hexadecimal representation of " ++ show n ++ " is " ++ decToHex n

Output

The hexadecimal representation of 52 is 34

Conclusion

In Haskell, a decimal number is converted to hexadecimal number using reverse, showIntAtBase and format functions. To convert a decimal number to hexadecimal, we divide the decimal number by 16 repeatedly, and use the remainder of each division as a hexadecimal digit. We continue dividing until the quotient is zero. The hexadecimal digits are arranged in reverse order to get the final hexadecimal representation.

Updated on: 13-Mar-2023

375 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements