Convert to, from and between positive numbers of different bases.
A type that contains the instructions to convert Ints into a string representing a non-decimal number and vice versa.
It's an opaque type.
b16 : Base
base16.
Classic hexadecimal.
> Base.fromInt b16 44000
"abe0" : String
This is case insensitive, meaning that if you use Base.toInt
it'll interpret upper and lower case input as the same.
> Base.toInt b16 "fe0f"
Ok 65039
> Base.toInt b16 "FE0F"
Ok 65039
b32 : Base
base32.
This is a slightly reduced subset of all numbers and letters,
leaving out w
, x
, y
, and z
.
> Base.fromInt b32 44000
"1av0"
This is case insensitive, meaning that if you use Base.toInt
it'll interpret upper and lower case input as the same.
b32rfc : Base
base32 in the RFC 4648 standard.
This is a slightly reduced subset of all numbers and letters, leaving out visually confusing combinations. The characters are ordered differently from normal b32.
> Base.fromInt b32rfc 44000
"bk7a"
This is case insensitive, meaning that if you use Base.toInt
it'll interpret upper and lower case input as the same.
b36 : Base
base36.
This is all numbers and letters in any case, making it both relatively dense and easy to manually type in.
> Base.fromInt b36 44000
"xy8"
This is case insensitive, meaning that if you use Base.toInt
it'll interpret upper and lower case input as the same.
> Base.toInt b36 "jeff"
Ok 905163
> Base.toInt b36 "JEFF"
Ok 905163
b58 : Base
base58.
This is all numbers and letters excluding visually confusing
combinations - there is no 0
, O
, I
or l
, leaving only
o
and 1
.
> Base.fromInt b58 44000
"E5d"
b62 : Base
base62.
All numbers and all cases of letters.
> Base.fromInt b62 44000
"BRg"
b64 : Base
base64 in the most common form for text.
All numbers and all cases of letters, plus 62nd and 63rd characters,
which are are +
and /
respectively.
> Base.fromInt b64 231166
"4b+"
b64url : Base
base64 in the RFC 4648 URL/filename-safe standard.
All numbers and all cases of letters, plus 62nd and 63rd characters,
which are are -
and _
respectively.
> Base.fromInt b64 231166
"4b-"
fromInt : Base -> Basics.Int -> String
Converts a positive Int into non-decimal number string.
> Base.fromInt b36 69420
"1hkc"
This does not support negative numbers - any negative numbers will be converted into positive numbers.
> Base.fromInt b36 -69420
"1hkc"
toInt : Base -> String -> Result Err Basics.Int
Converts a positive non-decimal number string into an Int.
> Base.toInt b64 "HAHA"
Ok 4498506
If your input string is invalid, it will return a
Err
. (See the docs for that type for more info.)
This does not support negative values and will likely throw an error if you introduce them (apart from b64, because '-' is one of it's characters.).
convert : Base -> Base -> String -> Result Err String
Converts the string of one non-decimal base number to another.
> Base.convert b16 b58 "8f0ce9"
Ok "q3r8"
If your input string is invalid, it will return a Err
.
(See the docs for that type for more info.)
Errors that may occur from converting from Strings.
> Base.toInt b16 ""
Err EmptyString
> Base.toInt b16 "owo"
Err (BadChars [(0,'o'),(1,'w'),(2,'o')])
EmptyString
- The given string is empty.BadChars
- There are characters in the given
string that don't match the spec of the base you used.
It provides a list of each char that failed along with their index in the input string.make : String -> CaseSensitivity -> Base
Create a base for non-decimal conversion.
The string input is both the characters you're mapping digits to (starting from digit 0) and the base of your resulting numbers (which will be the length of the string).
b16 : Base
b16 = make
"0123456789abcdef"
CaseAgnostic
Whether or not a base is case-sensitive. This affects whether cases
are relevant when the toInt
function is used.