base32-converter
v0.0.3
Published
Multi-system base 32 <-> binary conversion.
Downloads
54
Maintainers
Readme
base32-converter
Multi-system (including bring-your-own) base 32 <-> binary conversion.
Install
npm install base32-converter
Usage
var Base32Converter = require('base32-converter');
var converter = new Base32Converter(Base32Converter.system.RFC4648);
var val = converter.encode('10111');
console.log(val); // prints 'X'
console.log(converter.decode(val)); // prints '10111'
Documentation
Base32Converter([system])
Generates a new instance of the Base32Converter class. The system parameter can take multiple forms. If the system parameter is not supplied, the library will default to the Havi base 32 system. To use a built-in base 32 system, pass the system constant to the constructor.
Example
var convert = new Base32Converter(Base32Converter.system.Crockford);
Base32Converter also supports bringing your own system for base 32 conversion. There are two supported methods for doing this. You can either:
- Supply a string of 32 unique characters as the system parameter to the constructor.
- Create a custom system using an object hash. The parameters of the object are
validCharacters
,characterSubstitutions
, andpad
.
validCharacters
(string): String of 32 unique characters to use for the base 32 system. (required)characterSubstituions
(object): Object whose keys are characters you'd like replaced and value representing replacing characters. (optional)pad
(string): Character to use for padding. (optional)
Examples
// ---- Using a string
var convert = new Base32Converter('123456789ABCDEFGHIJKLMNOPQRSTUVW');
// ---- Using an object
var convert = new Base32Converter({
validCharacters: '123456789ABCDEFGHIJKLMNOPQRSTUVW',
characterSubstitutions: {
'0': 'O'
},
pad: '0'
});
Statics
Base32Converter.system
This represents an enum of native supported base 32 number systems. Below are the supported systems. Click the system for more information.
Base32Converter.system.Havi
Base32Converter.system.RFC4648
Base32Converter.system.z-base-32
Base32Converter.system.Crockford
Base32Converter.system.base32hex
Instance Methods
Base32Converter#decode(base32String)
This method will convert the base 32 string provided as the parameter and using the supplied conversion will convert it to base 2. Any characters that are supplied as part of the string that are not part of the designated system are ignored during conversion. Padding will be applied if supported by the system.
Base32Converter#encode(binaryString)
This method will convert the binary string provided as the parameter and using the supplied conversion will convert it to base 32. All characters that are not 0 or 1 will be removed before processing the string.
Havi Base 32 System
The Havi base 32 encoding system uses characters 0-9 and A-Z excluding I, L, O, Q. The characters I, L, and O where excluded due their similarity to 1 and 0. Q was excluded due to its representation in the phonetic alphabet. This system was developed to be used as a means by which to exchange simple encoded data over phones. The letter Q is represented as Quebec which has a 'k' sound as opposed to the 'q' sound. Also, the letter Q closely represents the appearance of 0 or O. In order to prevent unnecessary confusion, this character was excluded.