Haskell Program to convert Hexadecimal to Decimal


This tutorial will help us to creat a haskell program that can covert a given hexadecimal number to a decimal number using revers, map and fold1 functions

Hexadecimal to decimal conversion is the process of converting a number from the hexadecimal number system to the decimal number system.

The hexadecimal number system uses a base of 16, which means that there are 16 unique symbols used to represent numbers in this system (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F). The decimal number system, on the other hand, uses a base of 10, with the symbols 0 through 9 used to represent numbers.

Algorithm

  • Step 1 − The hexToDecimal function is defined using the internal function,

  • Step 2 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. In the main function, the hexToDecimal function is called with the argument and the result is printed to the console.

  • Step 3 − The variable named, “hexStr” is being initialized. It will hold the Hexadecimal number that is to be converted to respective decimal number.

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

Example 1

In this example, the hexToDecimal function first converts each character of the hexadecimal string to uppercase using map toUpper, then converts each character to its equivalent decimal value using map digitToInt, and finally calculates the decimal representation by summing up the products of the decimal values and their corresponding multipliers. The multipliers are calculated using iterate (*16) 1, which generates an infinite list of powers of 16, starting from 1. The zipWith (*) function is used to multiply each decimal value by its corresponding multiplier.

import Data.Char (digitToInt, toUpper)

hexToDecimal :: String -> Int
hexToDecimal = sum . zipWith (*) (iterate (*16) 1) . reverse . map digitToInt . map toUpper

main :: IO ()
main = do
   let hexStr = "34"
   putStrLn $ "The equivalent decimal representation is: " ++ show (hexToDecimal hexStr)

Output

The equivalent decimal representation is: 52

Example 2

In this method, the hexToDecimal function first converts each character of the hexadecimal string to uppercase using map toUpper, then calculates the decimal representation using the hexDigitToInt helper function, which maps each hexadecimal digit to its equivalent decimal value. The foldl function is then used to traverse the list of decimal values from left to right, and for each value it calculates the updated decimal representation by multiplying the current value by 16 and adding the decimal equivalent of the current hexadecimal digit.

import Data.Char (digitToInt, toUpper,ord)

hexToDecimal :: String -> Int
hexToDecimal = foldl (\acc x -> acc * 16 + hexDigitToInt x) 0 . map toUpper
   where hexDigitToInt x
          | x >= '0' && x <= '9' = ord x - ord '0'
          | x >= 'A' && x <= 'F' = ord x - ord 'A' + 10

main :: IO ()
main = do
   let hexStr = "34"
   putStrLn $ "The equivalent decimal representation is: " ++ show (hexToDecimal hexStr)

Output

The equivalent decimal representation is: 52

Conclusion

In Haskell, a hexadecimal number is converted to decimal number using reverse, map and fold1 functions. When converting from hexadecimal to decimal, each hexadecimal digit is multiplied by a power of 16, based on its position in the number. The decimal representation of a hexadecimal number is found by summing the decimal values of each digit.

Updated on: 06-Apr-2023

194 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements