Convert numbers from any base (radix) to any other base using any alphabet of digits.
// Flickr Base 58 alphabet.
const flickr58 = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
const dec2hex = AnyBase(AnyBase.dec, AnyBase.hex);
const dec2flickr = AnyBase(AnyBase.dec, flickr58);
dec2hex.convert('123456'); // returns '1e240'.
dec2flickr.convert('1234567890'); // returns '2T6u2h'.
dec2flickr.revert('2T6u2h'); // returns '1234567890'.
Returns an AnyBase object that can be used to convert input from the source alphabet to the destination alphabet, extrapolating the radices from the lengths of the alphabets.
const anyBase = const AnyBase(sourceAlphabet, destinationAlphabet);
String
sourceAlphabet digits in ascending order.String
destinationAlphabet digits in ascending order.
Ascending order meaning from lowest value to highest. For example
'0123456789'
in decimal.
Returns the input value mapped from the source alphabet to the
destination alphabet. For example, if the input value was '10'
and the
input alphabet was AnyBase.dec
(for decimal) and the output alphabet
was AnyBase.hex
(for hexadecimal) the return value would be 'a'
;
const anyBase = const AnyBase(sourceAlphabet, destinationAlphabet);
anyBase.convert(source);
String
source Input value as String. For example'10'
.
Performs the converse action of convert.
const anyBase = const AnyBase(sourceAlphabet, destinationAlphabet);
anyBase.revert(source);
String
source Input value as String. For example'a'
.
String
AnyBase.dec Predefined alphabet for decimal numbers.String
AnyBase.hex Predefined alphabet for hexadecimal numbers.String
AnyBase.bin Predefined alphabet for binary numbers.String
AnyBase.oct Predefined alphabet for octal numbers.
The original algorithm was converted from Kamil Harasimowicz's
any-base npm project. A
copy of the project's license can be found in the LICENSE
file under the section any-base algorithm
.