- Haskell Data Analysis Cookbook
- Nishant Shukla
- 364字
- 2021-12-08 12:43:38
Reading a number from another base
Decimal, binary, and hexadecimal are widely used numeral systems that are often represented using a string. This recipe will show how to convert a string representation of a number in an arbitrary base to its decimal integer. We use the readInt
function, which is the dual of the showIntAtBase
function described in the previous recipe.
How to do it...
- Import
readInt
and the following functions for character manipulation as follows:import Data.Char (ord, digitToInt, isDigit) import Numeric (readInt)
- Define a function to convert a string representing a number in a particular base to a decimal integer as follows:
str 'base' b = readInt b isValidDigit letterToNum str
- Define the mapping between letters and numbers for larger digits, as shown in the following code snippet:
letterToNum :: Char -> Int letterToNum d | isDigit d = digitToInt d | otherwise = ord d - ord 'a' + 10 isValidDigit :: Char -> Int isValidDigit d = letterToNum d >= 0
- Print out the result using the following line of codes:
main :: IO () main = do print $ "8" 'base' 12 print $ "a" 'base' 12 print $ "10" 'base' 12 print $ "3b" 'base' 12
- The printed output is as follows:
[(8,"")] [(10,"")] [(12,"")] [(47,"")]
How it works...
The readInt
function reads an unsigned integral value and converts it to the specified base. It takes in the base as the first argument, valid characters as the second argument, and its mapping from character to number as the third argument. We order our digits in the following order: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, and so on up to 36 characters. Putting it all together, we get a convenient way to convert a string representation of a number in an arbitrary base to a decimal number.
Tip
This recipe assumes that a valid string is passed into the base
function for conversion. Further error checks are necessary to ensure that erroneous input such as "a" 'base' 4
should not result in an answer.
See also
To do the reverse, refer to the Displaying a number in another base recipe.
- OpenStack Cloud Computing Cookbook(Third Edition)
- .NET之美:.NET關(guān)鍵技術(shù)深入解析
- Java程序設(shè)計(jì)實(shí)戰(zhàn)教程
- FreeSWITCH 1.8
- 大學(xué)計(jì)算機(jī)基礎(chǔ)實(shí)驗(yàn)教程
- Raspberry Pi Networking Cookbook(Second Edition)
- Windows Presentation Foundation Development Cookbook
- Visual C
- Mastering ROS for Robotics Programming
- 執(zhí)劍而舞:用代碼創(chuàng)作藝術(shù)
- INSTANT Adobe Edge Inspect Starter
- 21天學(xué)通C++(第5版)
- Java程序設(shè)計(jì)與項(xiàng)目案例教程
- 區(qū)塊鏈架構(gòu)之美:從比特幣、以太坊、超級賬本看區(qū)塊鏈架構(gòu)設(shè)計(jì)
- Docker:容器與容器云(第2版)