Skip to content

Instantly share code, notes, and snippets.

@gpedro
Forked from faisalman/baseConverter.js
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gpedro/4eec9ec0eff217ee9b45 to your computer and use it in GitHub Desktop.
Save gpedro/4eec9ec0eff217ee9b45 to your computer and use it in GitHub Desktop.
/**
* Convert From/To Binary/Decimal/Hexadecimal in JavaScript
* https://gist.github.com/faisalman
*
* Copyright 2012, Faisalman <fyzlman@gmail.com>
* Licensed under The MIT License
* http://www.opensource.org/licenses/mit-license
*/
(function(){
var convertBase = function (num) {
this.from = function (baseFrom) {
this.to = function (baseTo) {
return parseInt(num, baseFrom).toString(baseTo);
};
return this;
};
return this;
};
// binary to decimal
this.bin2dec = function (num) {
return convertBase(num).from(2).to(10);
};
// binary to hexadecimal
this.bin2hex = function (num) {
return convertBase(num).from(2).to(16);
};
// binary to octal
this.bin2oct = function (num) {
return convertBase(num).from(2).to(8);
};
// decimal to binary
this.dec2bin = function (num) {
return convertBase(num).from(10).to(2);
};
// decimal to hexadecimal
this.dec2hex = function (num) {
return convertBase(num).from(10).to(16);
};
// decimal to octal
this.dec2octal = function (num) {
return convertBase(num).from(10).to(8);
};
// hexadecimal to binary
this.hex2bin = function (num) {
return convertBase(num).from(16).to(2);
};
// hexadecimal to decimal
this.hex2dec = function (num) {
return convertBase(num).from(16).to(10);
};
// hexadecimal to octal
this.hex2dec = function (num) {
return convertBase(num).from(16).to(8);
};
return this;
})();
/*
* Usage example:
* bin2dec('111'); // '7'
* dec2hex('42'); // '2a'
* hex2bin('f8'); // '11111000'
* dec2bin('22'); // '10110'
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment