IntFormats- Convert integers in various bases to and from strings

Safe HaskellSafe



Includes functions for converting from a string to an integer, with the base determined automatically, and from an integer to a string, with the user specifying the base. Supports negative numbers.

For examples, see the test source

parseInt . showInt True _ == Right for all inputs, however the reverse is not necessarily true, even in the case of valid input, since parseInt accepts some rather creative capitalization that showInt does not produce.



data IntFormat Source #

IntFormat specifies which base is used for showInt



0d99, etc. This will be more commonly used without the prefix.


Hexadecimal with lowercase digits, such as 0xff


0o77, etc. In some programs, octal is denoted with a leading zero, however that is not used here.


0b11, etc.


Hexadecimal with uppercase digits. This only affects the digits A-F, not the prefix. For example, 0xFF

parseInt :: Integral a => String -> Either String a Source #

Converts a string to an integer if able, otherwise returns an error message. The number may begin with 0x, 0d, 0o, or 0b to specify the numerical base. If no prefix is provided, base 10 is assumed. The prefix is case insensitive. If the number is negative, the sign goes in front of the prefix. The number must not have leading spaces. The number may have characters following it, as long as it is not immediately followed by a digit, a letter, or a decimal point.

intParser :: (Integral a, Stream s m Char) => ParsecT s u m a Source #

The same as parseInt, except the parsec monad isn't evaluated. This is intended for use as part of a larger parser. If you simply want to get a value, use parseInt

showInt Source #


:: Integral a 
=> Bool

Whether to include the prefix 0x, 0d, 0o, or 0b. A value of True is generally recommended for bases other than decimal.

-> IntFormat

Which numerical base to use, and if it is hexadecimal, whether the digits are uppercase or lowercase

-> a

The number to be converted

-> String

The output value.

Converts a integer to a string, with the chosen numerical base.