Skip to content

Instantly share code, notes, and snippets.

@faisalman
Last active January 11, 2023 14:43

Revisions

  1. faisalman revised this gist Apr 12, 2015. 1 changed file with 30 additions and 27 deletions.
    57 changes: 30 additions & 27 deletions baseConverter.js
    Original file line number Diff line number Diff line change
    @@ -2,60 +2,63 @@
    * Convert From/To Binary/Decimal/Hexadecimal in JavaScript
    * https://gist.github.com/faisalman
    *
    * Copyright 2012, Faisalman <[email protected]>
    * Copyright 2012-2015, Faisalman <[email protected]>
    * 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;
    var ConvertBase = function (num) {
    return {
    from : function (baseFrom) {
    return {
    to : function (baseTo) {
    return parseInt(num, baseFrom).toString(baseTo);
    }
    };
    }
    };
    return this;
    };

    // binary to decimal
    this.bin2dec = function (num) {
    return convertBase(num).from(2).to(10);
    ConvertBase.bin2dec = function (num) {
    return ConvertBase(num).from(2).to(10);
    };

    // binary to hexadecimal
    this.bin2hex = function (num) {
    return convertBase(num).from(2).to(16);
    ConvertBase.bin2hex = function (num) {
    return ConvertBase(num).from(2).to(16);
    };

    // decimal to binary
    this.dec2bin = function (num) {
    return convertBase(num).from(10).to(2);
    ConvertBase.dec2bin = function (num) {
    return ConvertBase(num).from(10).to(2);
    };

    // decimal to hexadecimal
    this.dec2hex = function (num) {
    return convertBase(num).from(10).to(16);
    ConvertBase.dec2hex = function (num) {
    return ConvertBase(num).from(10).to(16);
    };

    // hexadecimal to binary
    this.hex2bin = function (num) {
    return convertBase(num).from(16).to(2);
    ConvertBase.hex2bin = function (num) {
    return ConvertBase(num).from(16).to(2);
    };

    // hexadecimal to decimal
    this.hex2dec = function (num) {
    return convertBase(num).from(16).to(10);
    ConvertBase.hex2dec = function (num) {
    return ConvertBase(num).from(16).to(10);
    };

    return this;
    })();
    this.ConvertBase = ConvertBase;

    })(this);

    /*
    * Usage example:
    * bin2dec('111'); // '7'
    * dec2hex('42'); // '2a'
    * hex2bin('f8'); // '11111000'
    * dec2bin('22'); // '10110'
    */
    * ConvertBase.bin2dec('111'); // '7'
    * ConvertBase.dec2hex('42'); // '2a'
    * ConvertBase.hex2bin('f8'); // '11111000'
    * ConvertBase.dec2bin('22'); // '10110'
    */
  2. faisalman revised this gist Dec 11, 2012. 1 changed file with 20 additions and 14 deletions.
    34 changes: 20 additions & 14 deletions baseConverter.js
    Original file line number Diff line number Diff line change
    @@ -7,49 +7,55 @@
    * http://www.opensource.org/licenses/mit-license
    */

    var convertBase = function (num) {
    (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
    bin2dec = function (num) {
    this.bin2dec = function (num) {
    return convertBase(num).from(2).to(10);
    },
    };

    // binary to hexadecimal
    bin2hex = function (num) {
    this.bin2hex = function (num) {
    return convertBase(num).from(2).to(16);
    },
    };

    // decimal to binary
    dec2bin = function (num) {
    this.dec2bin = function (num) {
    return convertBase(num).from(10).to(2);
    },
    };

    // decimal to hexadecimal
    dec2hex = function (num) {
    this.dec2hex = function (num) {
    return convertBase(num).from(10).to(16);
    },
    };

    // hexadecimal to binary
    hex2bin = function (num) {
    this.hex2bin = function (num) {
    return convertBase(num).from(16).to(2);
    },
    };

    // hexadecimal to decimal
    hex2dec = function (num) {
    this.hex2dec = function (num) {
    return convertBase(num).from(16).to(10);
    };

    return this;
    })();

    /*
    * Usage example:
    * bin2dec('111'); // '7'
    * dec2hex('42'); // '2a'
    * hex2bin('f8'); // '11111000'
    * dec2bin('22'); // '10110'
    */
  3. faisalman revised this gist Dec 5, 2012. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions baseConverter.js
    Original file line number Diff line number Diff line change
    @@ -17,26 +17,32 @@ var convertBase = function (num) {
    return this;
    },

    // binary to decimal
    bin2dec = function (num) {
    return convertBase(num).from(2).to(10);
    },

    // binary to hexadecimal
    bin2hex = function (num) {
    return convertBase(num).from(2).to(16);
    },

    // decimal to binary
    dec2bin = function (num) {
    return convertBase(num).from(10).to(2);
    },

    // decimal to hexadecimal
    dec2hex = function (num) {
    return convertBase(num).from(10).to(16);
    },

    // hexadecimal to binary
    hex2bin = function (num) {
    return convertBase(num).from(16).to(2);
    },

    // hexadecimal to decimal
    hex2dec = function (num) {
    return convertBase(num).from(16).to(10);
    };
  4. faisalman created this gist Dec 5, 2012.
    49 changes: 49 additions & 0 deletions baseConverter.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    /**
    * Convert From/To Binary/Decimal/Hexadecimal in JavaScript
    * https://gist.github.com/faisalman
    *
    * Copyright 2012, Faisalman <[email protected]>
    * Licensed under The MIT License
    * http://www.opensource.org/licenses/mit-license
    */

    var convertBase = function (num) {
    this.from = function (baseFrom) {
    this.to = function (baseTo) {
    return parseInt(num, baseFrom).toString(baseTo);
    };
    return this;
    };
    return this;
    },

    bin2dec = function (num) {
    return convertBase(num).from(2).to(10);
    },

    bin2hex = function (num) {
    return convertBase(num).from(2).to(16);
    },

    dec2bin = function (num) {
    return convertBase(num).from(10).to(2);
    },

    dec2hex = function (num) {
    return convertBase(num).from(10).to(16);
    },

    hex2bin = function (num) {
    return convertBase(num).from(16).to(2);
    },

    hex2dec = function (num) {
    return convertBase(num).from(16).to(10);
    };

    /*
    * Usage example:
    * bin2dec('111'); // '7'
    * dec2hex('42'); // '2a'
    * hex2bin('f8'); // '11111000'
    */