charcoder
Convert numbers into different numeral systems and back
Installation
npm i charcoder --save
Usage
Hexadecimal (0-9, a-f)
const Charcoder = const hex = hex // '64'hex // 100
Duosexagesimal (0-9, a-f, A-F)
const Charcoder B62 } = const b62 = B62 b62 // '1C'b62 // 100
Custom
You can use any characters to create your own numeral system. For example -
, +
and the word hey
.
const Charcoder = const custom = '-' + '+' + 'hey' custom // 'y--'custom // 100
Class: Charcoder
const Charcoder = // orconst Charcoder =
constructor(charset)
- charset
<String>
string that includes the characters of your numeral system. (default: Charcoder.HEX)
a = 'hi' + 'baz' acharset // 'hibaz'
Warning: Make sure that each character appears only once!
#encode(number)
- number
<Number>
number to convert
returns a string representing the number in the numeral system of the Charcoder.
#decode(string)
- string
<String>
string to convert
returns a number representing the value of the given string.
.NUM
A string that includes the numbers from 0 to 10.
CharcoderNUM // "0123456789"
.ABC
The whole alphabet in lowercase.
CharcoderABC // "abcdefghijklmnopqrstuvwxyz"
.HEX
All characters of the hexadecimal system (0-9, a-f).
CharcoderHEX // "0123456789abcdef"
.B62
All characters of the duosexagesimal system (0-9, a-f, A-F). The system consists of 62 characters.
CharcoderB62 // "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"