Class 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.
Inheritance
Namespace: Galaxon.Core.Numbers
Assembly: Galaxon.Core.dll
Syntax
public static class ConvertBase : object
Fields
Digits
Valid digits as a string, supporting up to base 64.
Declaration
public const string Digits = null
Field Value
| Type | Description |
|---|---|
| System.String |
MaxBase
The maximum base supported by the type.
Declaration
public const int MaxBase = null
Field Value
| Type | Description |
|---|---|
| System.Int32 |
MinBase
The minimum base supported by the type.
Declaration
public const int MinBase = null
Field Value
| Type | Description |
|---|---|
| System.Int32 |
ThinSpace
Unicode thin space character. Can be useful for formatting numbers.
Declaration
public const char ThinSpace = null
Field Value
| Type | Description |
|---|---|
| System.Char |
Methods
FromBase<T>(String, Byte)
Convert a string of digits in a given base into a int. Group separator characters, including spaces, newlines, thin spaces, periods, commas, and underscores, will be ignored.
Declaration
public static T FromBase<T>(string digits, byte fromBase)
where T : IBinaryInteger<T>
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | digits | A string of digits in the specified base. |
| System.Byte | fromBase | The base that the digits in the string are in. |
Returns
| Type | Description |
|---|---|
| T | The integer equivalent of the digits. |
Type Parameters
| Name | Description |
|---|---|
| T | The target type to create an instance of. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentFormatException | If string contains invalid characters for the specified base. |
FromBin<T>(String)
Convert a string of binary digits into an integer.
Declaration
public static T FromBin<T>(string digits)
where T : IBinaryInteger<T>
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | digits | The string of digits to parse. |
Returns
| Type | Description |
|---|---|
| T | The integer equivalent of the digits. |
Type Parameters
| Name | Description |
|---|---|
| T | The integer type to create. |
FromHex<T>(String)
Convert a string of hexadecimal digits into an integer.
Declaration
public static T FromHex<T>(string digits)
where T : IBinaryInteger<T>
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | digits | The string of digits to parse. |
Returns
| Type | Description |
|---|---|
| T | The integer equivalent of the digits. |
Type Parameters
| Name | Description |
|---|---|
| T | The integer type to create. |
FromOct<T>(String)
Convert a string of octal digits into an integer.
Declaration
public static T FromOct<T>(string digits)
where T : IBinaryInteger<T>
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | digits | The string of digits to parse. |
Returns
| Type | Description |
|---|---|
| T | The integer equivalent of the digits. |
Type Parameters
| Name | Description |
|---|---|
| T | The integer type to create. |
FromQuat<T>(String)
Convert a string of quaternary digits into an integer.
Declaration
public static T FromQuat<T>(string digits)
where T : IBinaryInteger<T>
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | digits | The string of digits to parse. |
Returns
| Type | Description |
|---|---|
| T | The integer equivalent of the digits. |
Type Parameters
| Name | Description |
|---|---|
| T | The integer type to create. |
FromTetra<T>(String)
Convert a string of tetrasexagesimal digits into an integer.
Declaration
public static T FromTetra<T>(string digits)
where T : IBinaryInteger<T>
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | digits | The string of digits to parse. |
Returns
| Type | Description |
|---|---|
| T | The integer equivalent of the digits. |
Type Parameters
| Name | Description |
|---|---|
| T | The integer type to create. |
FromTria<T>(String)
Convert a string of triacontakaidecimal digits into an integer.
Declaration
public static T FromTria<T>(string digits)
where T : IBinaryInteger<T>
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | digits | The string of digits to parse. |
Returns
| Type | Description |
|---|---|
| T | The integer equivalent of the digits. |
Type Parameters
| Name | Description |
|---|---|
| T | The integer type to create. |
ToBase<T>(T, Byte, Nullable<Boolean>)
Convert an integer to a string of digits in a given base.
Note, a negative value will be converted to a non-negative value with the same underlying bits. This reflects the behaviour of other base-conversion methods in .NET.
Declaration
public static string ToBase<T>(this T n, byte toBase, bool? letterCase = null)
where T : IBinaryInteger<T>
Parameters
| Type | Name | Description |
|---|---|---|
| T | n | The instance value. |
| System.Byte | toBase | The base to convert to. |
| System.Nullable<System.Boolean> | letterCase | If letters should be lower-case, upper-case, or default. null = default (all lower-case except for L; see Digits) true = upper-case false = lower-case |
Returns
| Type | Description |
|---|---|
| System.String | The string of digits. |
Type Parameters
| Name | Description |
|---|---|
| T | The integer type. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentInvalidException | If T is an unsupported type. |
ToBin<T>(T)
Convert an integer to binary digits.
Declaration
public static string ToBin<T>(this T n)
where T : IBinaryInteger<T>
Parameters
| Type | Name | Description |
|---|---|---|
| T | n | The integer to convert. |
Returns
| Type | Description |
|---|---|
| System.String | The value as a string of binary digits. |
Type Parameters
| Name | Description |
|---|---|
| T |
ToHex<T>(T, Nullable<Boolean>)
Convert integer to hexadecimal digits.
Declaration
public static string ToHex<T>(this T n, bool? letterCase = null)
where T : IBinaryInteger<T>
Parameters
| Type | Name | Description |
|---|---|---|
| T | n | The integer to convert. |
| System.Nullable<System.Boolean> | letterCase | If letters should be lower-case, upper-case, or default. |
Returns
| Type | Description |
|---|---|
| System.String | The value as a string of hexadecimal digits. |
Type Parameters
| Name | Description |
|---|---|
| T |
ToOct<T>(T)
Convert integer to octal digits.
Declaration
public static string ToOct<T>(this T n)
where T : IBinaryInteger<T>
Parameters
| Type | Name | Description |
|---|---|---|
| T | n | The integer to convert. |
Returns
| Type | Description |
|---|---|
| System.String | The value as a string of octal digits. |
Type Parameters
| Name | Description |
|---|---|
| T |
ToQuat<T>(T)
Convert integer to quaternary digits.
Declaration
public static string ToQuat<T>(this T n)
where T : IBinaryInteger<T>
Parameters
| Type | Name | Description |
|---|---|---|
| T | n | The integer to convert. |
Returns
| Type | Description |
|---|---|
| System.String | The value as a string of quaternary digits. |
Type Parameters
| Name | Description |
|---|---|
| T |
ToTetra<T>(T, Nullable<Boolean>)
Convert integer to tetrasexagesimal (base 64) digits.
Declaration
public static string ToTetra<T>(this T n, bool? letterCase = null)
where T : IBinaryInteger<T>
Parameters
| Type | Name | Description |
|---|---|---|
| T | n | The integer to convert. |
| System.Nullable<System.Boolean> | letterCase | If letters should be lower-case, upper-case, or default. |
Returns
| Type | Description |
|---|---|
| System.String | The value as a string of tetrasexagesimal digits. |
Type Parameters
| Name | Description |
|---|---|
| T |
ToTria<T>(T, Nullable<Boolean>)
Convert integer to triacontakaidecimal (base 32) digits.
Declaration
public static string ToTria<T>(this T n, bool? letterCase = null)
where T : IBinaryInteger<T>
Parameters
| Type | Name | Description |
|---|---|---|
| T | n | The integer to convert. |
| System.Nullable<System.Boolean> | letterCase | If letters should be lower-case, upper-case, or default. |
Returns
| Type | Description |
|---|---|
| System.String | The value as a string of triacontakaidecimal digits. |
Type Parameters
| Name | Description |
|---|---|
| T |