Galaxon.Core.Numbers
Classes
ConvertBase
This class supports conversion between integers and strings of digits in a specified base, which can be in the range 2..64.
All built-in integer types are supported, including Int128, UInt128, and BigInteger.
Most languages support conversion to and from base 2 to 36, formed from the 10 digits plus the 26 letters in the English alphabet. To this sequence I've appended 28 non-alphanumeric (symbol) ASCII characters in order to extend the range of supported bases to 64. The symbols are appended to the standard 36 digits in ASCII value order. These characters only require 1 byte in UTF-8 and I assume all, or almost all, computer keyboards will support all of them.
This has been done to add support for base 64, mainly for my own amusement, and to provide an alternative to bog-standard Base64 encoding (see link). The difference here, if it matters, is that the base 64 digits used here are compatible with hexadecimal digit values, and all other bases.
ASCII provides 32 symbol (or punctuation) characters. The 4 omitted characters are '.' (period), ',' (comma), and '_' (underscore) because these can all be used as group separators (in different contexts), and '"' (double quote) which is the string delimiter in C#.
As in hexadecimal literals, upper-case letters have the same value as lower-case letters. Use the parameter "letterCase" to specify for the result to use all lower- or all upper-case letters. See the method documentation for ToBase() for how to use this parameter.
The default is to use lower-case for all letters except for L (see the Digits constant). Upper-case letters are more easily confused with numerals than lower-case. For example:
- 'O' looks like '0'
- 'I' looks like '1'
- 'Z' looks like '2'
- 'S' looks like '5'
- 'G' looks like '6'
- 'B' looks like '8' The only similar problem with lower-case letters is that 'l' looks like '1'. To solve this, upper-case is used for this letter only. (This is the same reason why we use "L" as a suffix for long literals in C#, as a rule.)
These days, most fonts, especially those used by IDEs, make it easy enough to distinguish between letters and numbers, so it's not the issue it once was. Multiple coding standards for CSS require lower-case hex digits in color literals. "L" is not a hexadecimal digit, so this behaviour doesn't violate that standard. Other than that, I can't find any standards that mandate one over the other. It seems upper-case is favoured in older languages, lower-case in newer.
The core methods are ToBase() and FromBase(). In addition, convenience methods are provided in the form of "To" and "From" methods for all bases that are a power of 2:
|------------------------|--------|----------------| | Numeral system | Base | Abbreviation | |------------------------|--------|----------------| | binary | 2 | Bin | | quaternary | 4 | Quat | | octal | 8 | Oct | | hexadecimal | 16 | Hex | | triacontakaidecimal* | 32 | Tria | | tetrasexagesimal | 64 | Tetra | |------------------------|--------|----------------|
*Base 32 is more correctly called "duotrigesimal". However, there are multiple methods in use for encoding base 32 digits; the one used here is called "triacontakaidecimal" (see link below), also known as "base32hex". It's the same encoding used in Java in JavaScript.
XBigInteger
Extension methods for BigInteger.
XBinaryInteger
Extension methods for IBinaryInteger{T}.
XDecimal
Extension methods for decimal.
XDouble
Extension methods and other bonus stuff for double.
XFloat
Extension methods and other bonus stuff for float.
XFloatingPoint
Extension methods for IFloatingPoint{T}.
XHalf
Extension methods and other bonus stuff for Half.
XInt
Extension methods for int.
XLong
Extension methods for long.
XNumber
Extension methods for numbers (INumber{T} and INumberBase{T}).
XSbyte
Extension methods for sbyte.
XShort
Extension methods for short.
XUint
Extension methods for uint.
XUlong
Extension methods for ulong.
XUshort
Extension methods for ushort.