encode-x
Infinite base encoder/decoder. Can handle bases up to full 64 bit floating points Enough to represent the entire bitcoin private key space with at most 3 characters!
const C = ; // Without the trailing () you would do c(fromAlphabet, toAlphabet).fromXXXToYYY(data) C; // 'f'C; // 'P'C; // '/'C; // '¿'C; // '½Ǥɰ:ɧźaM)ûȭljĎʍ9ĿƢȷ'C; // 'The devil says, SICK!' // fromDataToXXX and fromTextToXXX are synonyms for fromUTF8ToXXX// likewise one can do fromXXXtoData, and fromXXXtoText.
In case you come to need the default alphabet(if unspecified, and no precomputed base matches), its mapped with a simple unicode loop.
...ArrayAMOUNT_DESIRED+=48
As you can tell, the module works for all bases and uses a Proxy to capture the methods, they are not actually all defined on the prototype.
The core algorithm uses modular division and bitshifting exponentiation logic applied directly to buffer streams. It is from my experience the only working full base conversion module that is both practical and scales to a high degree. For the most part, the code is original.
For full documentation see the encode-x full docs.
Features
1.) In built alphabets, up to base 1 million plus by default.
2.) setGlobalAlphabet
, setFromAlphabet
, setToAlphabet
, and resetAlphabets
API for ease.
3.) Ability to parse text data to and from.
4.) Ability to encode and decode streems of pure 0's. (usually lost data in encodings).
5.) Ability to encode/decode from/to using a base different than the incoming/outgoing base.
6.) For example using the above feature we can swap from alphabet 1 to 2 using from10To10
.
7.) A dumpAlphabets
command to conveniently store your favorite, or needed alphabets.
You may also require and call the object directly (like bellow), or even link to it from a browser.
reply = /* [from alphabet], [to alphabet] */ /* OR */ C = CsetToAlphabet = CsetFromAlphabet = "1234567890abcdefgahiklm"C // Error is thrown because the alphabet it to small for base999. /* OR */ ;
Instalation
npm install encode-x
Dependencies
None.
THE BELLOW CODE IS NOT PART OF ENCODE-X
It is what inspired me to go past the memory limits
Example using this modules core logic to parse rgb[a] data into hex..
/* Unless the regular expressions are modified accordingly...* data should be of the format "rgba(213, 11, 0, 70)" or "rgb(113, 81, 70)"* returns "#d50b0046" and "#4286f4" respectively. */ { var digits = cssRGB; var alphabet = "0123456789abcdef"; var base = alphabetlength; // Pop off the rgb ALPHA slot if its not present. digits0 === undefined && digits; var final = ; forlet i = 0 l = digitslength; i < l; ++i let digit = digitsi res = ; do res; digit = Math|0 // |0 for NaN whiledigit; // Create padding if the rgba # was under 0x10 res; final += res; res = return '#' + final}
The above gist has been battle tested, the bellow is purely me typing into the README as an example. Extended a much less than the module, but enough for the curious mind.
{ // Assume "255" ifraw return this = raw; this_raw = Buffer || null; // Our character map (up to base ~65411). thisalphabet = null; this_alphabet = return ...Arrays+=48 }Convertprototype var alphabet = thisalphabet || thisalphabet = this; var digits = this_raw || null; var base = base || thisalphabetlength; ifdigits try digits = Buffer