fredcy / elm-parseint / ParseInt

Convert String value to Int, or Int to String, with given radix.

Functions

parseInt : String -> Result Error Basics.Int

Convert String to Int assuming base 10.

parseInt "314159" == Ok 314159
parseInt "foo" = Err (OutOfRange 'o')

parseIntOct : String -> Result Error Basics.Int

Convert String to Int assuming base 8 (octal). No leading '0' is required.

parseIntHex : String -> Result Error Basics.Int

Convert String to Int assuming base 16 (hexadecimal). No leading characters are expected; input starting with "0x" (or any other out of range character) will cause an Err return.

parseIntRadix : Basics.Int -> String -> Result Error Basics.Int

Convert String to Int assuming given radix. Radix can be any of 2..36. Leading zeroes are ignored. Valid characters are the alphanumerics: those in the ASCII range [0-9a-zA-Z]. Case does not matter. For radixes beyond 16 the normal [A-F] range for hexadecimal is extended in the natural way. Any invalid character results in a Err return. Any valid character outside of the range defined by the radix also results in an Err. In particular, any initial '-' or ' ' (space) is an error. An Ok return means that the entire input string was consumed. The empty string results in Ok 0

parseIntRadix 16 "DEADBEEF" =
    Ok 3735928559

toRadix : Basics.Int -> Basics.Int -> Result Error String

Convert Int to String assuming given radix. Radix values from 2..36 are allowed; others result in an Err InvalidRadix. Negative numbers get an initial '-'.

toRadix 16 1234 == Ok "4D2"

toRadix 8 -99 == Ok "-143"

toRadixUnsafe : Basics.Int -> Basics.Int -> String

Convert Int to String assuming given radix. Radix value must be in 2..36 (not checked, so it can crash).

toRadixUnsafe 16 3735928559 == "DEADBEEF"

toRadixUnsafe 37 36 --> crash

toOct : Basics.Int -> String

Convert Int to octal String.

toHex : Basics.Int -> String

Convert Int to hexadecimal String.

intFromChar : Basics.Int -> Char -> Result Error Basics.Int

Convert an alphanumeric character to an int value as a "digit", validating against the given radix. Alphabetic characters past "F" are extended in the natural way: 'G' == 16, 'H' == 17, etc. Upper and lower case are treated the same. Passing a non-alphanumeric character results in the InvalidChar error. If the resulting value would be greater than the given radix, an OutOfRange error results instead.

charFromInt : Basics.Int -> Char

Convert Int to corresponding Char representing it as a digit. Values from 10..15 are represented as upper-case 'A'..'F'. Values 16 and above extend the hexadecimal characters in the natural way. This function assumes that the input value is in the range 0 .. 36.

Errors


type Error
    = InvalidChar Char
    | OutOfRange Char
    | InvalidRadix Basics.Int

Possible Result.Err returns from these functions.