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
-
-
Save faisalman/4213592 to your computer and use it in GitHub Desktop.
Convert From/To Binary/Decimal/Hexadecimal in JavaScript
This file contains 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 characters
/** | |
* 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' | |
*/ |
To convert large binary to hex use this code
https://gist.github.com/Aravin/011f17528f90be6bb4f5a4cb253647d6
based on the suggestion of @SeanCannon I made this conversion from utf8 to binary, I just tested node
const convert = {
utfBin: (s) => parseInt(Buffer.from(s, "utf-8").toString("hex"), 16).toString(2),
binUtf: (s) => Buffer.from(parseInt(s, 2).toString(16), "hex").toString("utf-8"),
};
const bin = convert.utfBin("1808z");
console.log("utfBin: ", bin);
console.log("binUtf: ", convert.binUtf(bin));
thanks very much. i like this project so.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
POST /wifi HTTP/1.1
Host: 52.15.64.34:8080
Content-Type:application/json; charset=utf-8
Content-Length: 835
{"route_mac":"FC596736DF23","devices":[{"mac":"444B9A6C5C88","data":"02011A0AFF4C0010050B10C909AB","rssi":-35},{"mac":"08DF1F99F8B7","data":"0201020303BEFE0DFF1001400C0141F45C89AE4C1E","rssi":-65},{"mac":"D72BD6E59DF5","data":"0201061AFF4C000215F7826DA64FA24E988024BC5B71E0893E12345678B3","rssi":-81},{"mac":"D84694191046","data":"0201041BFF4C000215EBEFD08370A247C89837E7B5634DF52411110000C55D","rssi":-34},{"mac":"D960ADD80C37","data":"0201041BFF4C000215EBEFD08370A247C89837E7B5634DF524D8ADE70CCB5E","rssi":-30},{"mac":"D5F2FC26189E","data":"020105020A000303E7FE09FF4EABD5F2FC26189E050948573031","rssi":-79},{"mac":"CF9CDD68F3DF","data":"0201041BFF4C000215EBEFD08370A247C89837E7B5634DF5241111E000C55C","rssi":-35},{"mac":"C7406B446830","data":"0201041BFF4C000215EBEFD08370A247C89837E7B5634DF5241111E000C55E","rssi":-40}],"allCount":8}