Last active
January 11, 2023 14:43
-
Star
(224)
You must be signed in to star a gist -
Fork
(70)
You must be signed in to fork a gist
Revisions
-
faisalman revised this gist
Apr 12, 2015 . 1 changed file with 30 additions and 27 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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-2015, Faisalman <[email protected]> * Licensed under The MIT License * http://www.opensource.org/licenses/mit-license */ (function(){ var ConvertBase = function (num) { return { from : function (baseFrom) { return { to : function (baseTo) { return parseInt(num, baseFrom).toString(baseTo); } }; } }; }; // binary to decimal ConvertBase.bin2dec = function (num) { return ConvertBase(num).from(2).to(10); }; // binary to hexadecimal ConvertBase.bin2hex = function (num) { return ConvertBase(num).from(2).to(16); }; // decimal to binary ConvertBase.dec2bin = function (num) { return ConvertBase(num).from(10).to(2); }; // decimal to hexadecimal ConvertBase.dec2hex = function (num) { return ConvertBase(num).from(10).to(16); }; // hexadecimal to binary ConvertBase.hex2bin = function (num) { return ConvertBase(num).from(16).to(2); }; // hexadecimal to decimal ConvertBase.hex2dec = function (num) { return ConvertBase(num).from(16).to(10); }; this.ConvertBase = ConvertBase; })(this); /* * Usage example: * ConvertBase.bin2dec('111'); // '7' * ConvertBase.dec2hex('42'); // '2a' * ConvertBase.hex2bin('f8'); // '11111000' * ConvertBase.dec2bin('22'); // '10110' */ -
faisalman revised this gist
Dec 11, 2012 . 1 changed file with 20 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,49 +7,55 @@ * 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); }; // 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); }; // 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); }; return this; })(); /* * Usage example: * bin2dec('111'); // '7' * dec2hex('42'); // '2a' * hex2bin('f8'); // '11111000' * dec2bin('22'); // '10110' */ -
faisalman revised this gist
Dec 5, 2012 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); }; -
faisalman created this gist
Dec 5, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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' */