Skip to content

robojones/charcoder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

charcoder

Convert numbers into different numeral systems and back

Build Status Test Coverage

bitHound Code bitHound Overall Score bitHound Dependencies bitHound Dev Dependencies License: MIT

Installation

npm i charcoder --save

Usage

Hexadecimal (0-9, a-f)

const Charcoder = require('charcoder')

const hex = new Charcoder()

hex.encode(100) // '64'
hex.decode('64') // 100

Duosexagesimal (0-9, a-f, A-F)

const {
  Charcoder,
  B62
} = require('charcoder')

const b62 = new Charcoder(B62)

b62.encode(100) // '1C'
b62.decode('1C') // 100

Custom

You can use any characters to create your own numeral system. For example -, + and the word hey.

const Charcoder = require('charcode')

const custom = new Charcoder('-' + '+' + 'hey')

custom.encode(100) // 'y--'
custom.decode('y--') // 100

Class: Charcoder

const Charcoder = require('charcoder')
// or
const { Charcoder } = require('charcoder')

constructor(charset)

  • charset <String> string that includes the characters of your numeral system. (default: Charcoder.HEX)
a = new Charcoder('hi' + 'baz')

a.charset // '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.

Charcoder.NUM // "0123456789"

.ABC

The whole alphabet in lowercase.

Charcoder.ABC // "abcdefghijklmnopqrstuvwxyz"

.HEX

All characters of the hexadecimal system (0-9, a-f).

Charcoder.HEX // "0123456789abcdef"

.B62

All characters of the duosexagesimal system (0-9, a-f, A-F). The system consists of 62 characters.

Charcoder.B62 // "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"