-
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 | |
* 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' | |
*/ |
thanks a lot i modified few things out for my project check the link: http://vslcreations.in/projects/numbersystem.html
These feel bloated. Here's a trimmed down ES6 version. I didn't check or refactor the logic at all:
const convert = {
bin2dec : s => parseInt(s, 2).toString(10),
bin2hex : s => parseInt(s, 2).toString(16),
dec2bin : s => parseInt(s, 10).toString(2),
dec2hex : s => parseInt(s, 10).toString(16),
hex2bin : s => parseInt(s, 16).toString(2),
hex2dec : s => parseInt(s, 16).toString(10)
};
convert.bin2dec('111'); // '7'
convert.dec2hex('42'); // '2a'
convert.hex2bin('f8'); // '11111000'
convert.dec2bin('22'); // '10110'
I turned this into an NPM library if anybody wants to use it : https://www.npmjs.com/package/aybabtu
I guess this version is cleaner a bit
const convert = (baseFrom, baseTo) => number => parseInt(number, baseFrom).toString(baseTo);
const bin2dec = convert(2, 10);
const dec2bin = convert(10, 2);
bin2dec('111'); // '7'
@Flyr1Q - const convert
function syntax - took me some time to get my head around that line!!
@Flyr1Q genius
i am getting hex from a device - 50 4F 53 54 20 2F 77 69 66 69 20 48 54 54 50 2F 31 2E 31 0D 0A 48 6F 73 74 3A 20 35 32 2E 31 35 2E 36 34 2E 33 34 3A 38 30 38 30 0D 0A 43 6F 6E 74 65 6E 74 2D 54 79 70 65 3A 61 70 70 6C 69 63 61 74 69 6F 6E 2F 6A 73 6F 6E 3B 20 63 68 61 72 73 65 74 3D 75 74 66 2D 38 0D 0A 43 6F 6E 74 65 6E 74 2D 4C 65 6E 67 74 68 3A 20 38 33 35 0D 0A 0D 0A 7B 22 72 6F 75 74 65 5F 6D 61 63 22 3A 22 46 43 35 39 36 37 33 36 44 46 32 33 22 2C 22 64 65 76 69 63 65 73 22 3A 5B 7B 22 6D 61 63 22 3A 22 34 34 34 42 39 41 36 43 35 43 38 38 22 2C 22 64 61 74 61 22 3A 22 30 32 30 31 31 41 30 41 46 46 34 43 30 30 31 30 30 35 30 42 31 30 43 39 30 39 41 42 22 2C 22 72 73 73 69 22 3A 2D 33 35 7D 2C 7B 22 6D 61 63 22 3A 22 30 38 44 46 31 46 39 39 46 38 42 37 22 2C 22 64 61 74 61 22 3A 22 30 32 30 31 30 32 30 33 30 33 42 45 46 45 30 44 46 46 31 30 30 31 34 30 30 43 30 31 34 31 46 34 35 43 38 39 41 45 34 43 31 45 22 2C 22 72 73 73 69 22 3A 2D 36 35 7D 2C 7B 22 6D 61 63 22 3A 22 44 37 32 42 44 36 45 35 39 44 46 35 22 2C 22 64 61 74 61 22 3A 22 30 32 30 31 30 36 31 41 46 46 34 43 30 30 30 32 31 35 46 37 38 32 36 44 41 36 34 46 41 32 34 45 39 38 38 30 32 34 42 43 35 42 37 31 45 30 38 39 33 45 31 32 33 34 35 36 37 38 42 33 22 2C 22 72 73 73 69 22 3A 2D 38 31 7D 2C 7B 22 6D 61 63 22 3A 22 44 38 34 36 39 34 31 39 31 30 34 36 22 2C 22 64 61 74 61 22 3A 22 30 32 30 31 30 34 31 42 46 46 34 43 30 30 30 32 31 35 45 42 45 46 44 30 38 33 37 30 41 32 34 37 43 38 39 38 33 37 45 37 42 35 36 33 34 44 46 35 32 34 31 31 31 31 30 30 30 30 43 35 35 44 22 2C 22 72 73 73 69 22 3A 2D 33 34 7D 2C 7B 22 6D 61 63 22 3A 22 44 39 36 30 41 44 44 38 30 43 33 37 22 2C 22 64 61 74 61 22 3A 22 30 32 30 31 30 34 31 42 46 46 34 43 30 30 30 32 31 35 45 42 45 46 44 30 38 33 37 30 41 32 34 37 43 38 39 38 33 37 45 37 42 35 36 33 34 44 46 35 32 34 44 38 41 44 45 37 30 43 43 42 35 45 22 2C 22 72 73 73 69 22 3A 2D 33 30 7D 2C 7B 22 6D 61 63 22 3A 22 44 35 46 32 46 43 32 36 31 38 39 45 22 2C 22 64 61 74 61 22 3A 22 30 32 30 31 30 35 30 32 30 41 30 30 30 33 30 33 45 37 46 45 30 39 46 46 34 45 41 42 44 35 46 32 46 43 32 36 31 38 39 45 30 35 30 39 34 38 35 37 33 30 33 31 22 2C 22 72 73 73 69 22 3A 2D 37 39 7D 2C 7B 22 6D 61 63 22 3A 22 43 46 39 43 44 44 36 38 46 33 44 46 22 2C 22 64 61 74 61 22 3A 22 30 32 30 31 30 34 31 42 46 46 34 43 30 30 30 32 31 35 45 42 45 46 44 30 38 33 37 30 41 32 34 37 43 38 39 38 33 37 45 37 42 35 36 33 34 44 46 35 32 34 31 31 31 31 45 30 30 30 43 35 35 43 22 2C 22 72 73 73 69 22 3A 2D 33 35 7D 2C 7B 22 6D 61 63 22 3A 22 43 37 34 30 36 42 34 34 36 38 33 30 22 2C 22 64 61 74 61 22 3A 22 30 32 30 31 30 34 31 42 46 46 34 43 30 30 30 32 31 35 45 42 45 46 44 30 38 33 37 30 41 32 34 37 43 38 39 38 33 37 45 37 42 35 36 33 34 44 46 35 32 34 31 31 31 31 45 30 30 30 43 35 35 45 22 2C 22 72 73 73 69 22 3A 2D 34 30 7D 5D 2C 22 61 6C 6C 43 6F 75 6E 74 22 3A 38 7D 0D 0A 0D 0A
and i am trying to convert it to binary but but getting 1000111
@jimmylogic you can decode your long hex data through this online tool : https://www.useotools.com/decode-hex
Comprehensive code!!!!! Like it.
ghost
Excellent Code !!!! Like it.
Excellent Code !!!
Maybe do you have things, how works converter to decimal from text? As example 'H'.charCodeAt(0); \ 72 , if i want convert 'Hello everyone' to decimal it was be - 72 101 108 108 111 32 116 104 101 114 101 33
How it was be in a code?
let textcode = prompt('enter text for hash');
String.prototype.hashCode = function() {
if (Array.prototype.reduce) {
return this.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0);
} else {
var hash = 0, i, chr, len;
if (this.length == 0) return hash;
for (i = 0, len = this.length; i < len; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
}
};
textcode.hashCode();
let number = textcode.hashCode();
function opposite(number) {
if (number < 0) {
return -(number)
} else {
return +(number)
}
}
alert(opposite(number));
Is your code just for unsigned numbers?
What should be done if we have signed hexadecimal numbers?
<!doctype html>
<style> textarea{ min-width:27%; min-height:150px; font-size: 20px; }
button{
padding: 5px;
font-size: 20px;
}
</style>
<textarea id="strInput" placeholder="Stirn input"></textarea>
!StrToBin
<textarea id="binOutput" placeholder="Binary input/output"></textarea>!BinToStr
<textarea id="strOutput" placeholder="Binary to Stirng Output"></textarea> <textarea id="decOutput" placeholder="Decimal/ascci Output"></textarea>!BinToDec
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $('#listener1').bind('click',function(){ let value = $('#strInput').val(); let binary = function(val){ let sb = ''; for(let i=0; i-1; i--){ if(parseFloat( intValue.charAt((intValue.length-1)-i)) === 0){continue;} int += Math.pow(2,i); } return int; } function forFloat(floatValue){ let float = 0 for(let i=0; i<!doctype html>
<style> textarea{ min-width:27%; min-height:150px; font-size: 20px; }
button{
padding: 5px;
font-size: 20px;
}
</style>
<textarea id="strInput" placeholder="String input"></textarea>
!StrToBin
<textarea id="binOutput" placeholder="Binary input/output"></textarea>!BinToStr
<textarea id="strOutput" placeholder="Binary to Stirng Output"></textarea> <textarea id="decOutput" placeholder="Decimal/ascii Output"></textarea>!BinToDec
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $('#listener1').bind('click',function(){ let value = $('#strInput').val(); let binary = function(val){ let sb = ''; for(let i=0; i-1; i--){ if(parseFloat( intValue.charAt((intValue.length-1)-i)) === 0){continue;} int += Math.pow(2,i); } return int; } function forFloat(floatValue){ let float = 0 for(let i=0; iI can't send my source code as comment here. check out my this repository
https://github.com/shantoislam6/binaryToDec-Ascii-To-String
What is the best way to convert your HEX into a DWORD? In particular I have a uint coming from a device. JS ToInt32()
does not seem to do the trick.
so FA7B 8524
should equal -92568284
and not equal 4202399012
50 4F 53 54 20 2F 77 69 66 69 20 48 54 54 50 2F 31 2E 31 0D 0A 48 6F 73 74 3A 20 35 32 2E 31 35 2E 36 34 2E 33 34 3A 38 30 38 30 0D 0A 43 6F 6E 74 65 6E 74 2D 54 79 70 65 3A 61 70 70 6C 69 63 61 74 69 6F 6E 2F 6A 73 6F 6E 3B 20 63 68 61 72 73 65 74 3D 75 74 66 2D 38 0D 0A 43 6F 6E 74 65 6E 74 2D 4C 65 6E 67 74 68 3A 20 38 33 35 0D 0A 0D 0A 7B 22 72 6F 75 74 65 5F 6D 61 63 22 3A 22 46 43 35 39 36 37 33 36 44 46 32 33 22 2C 22 64 65 76 69 63 65 73 22 3A 5B 7B 22 6D 61 63 22 3A 22 34 34 34 42 39 41 36 43 35 43 38 38 22 2C 22 64 61 74 61 22 3A 22 30 32 30 31 31 41 30 41 46 46 34 43 30 30 31 30 30 35 30 42 31 30 43 39 30 39 41 42 22 2C 22 72 73 73 69 22 3A 2D 33 35 7D 2C 7B 22 6D 61 63 22 3A 22 30 38 44 46 31 46 39 39 46 38 42 37 22 2C 22 64 61 74 61 22 3A 22 30 32 30 31 30 32 30 33 30 33 42 45 46 45 30 44 46 46 31 30 30 31 34 30 30 43 30 31 34 31 46 34 35 43 38 39 41 45 34 43 31 45 22 2C 22 72 73 73 69 22 3A 2D 36 35 7D 2C 7B 22 6D 61 63 22 3A 22 44 37 32 42 44 36 45 35 39 44 46 35 22 2C 22 64 61 74 61 22 3A 22 30 32 30 31 30 36 31 41 46 46 34 43 30 30 30 32 31 35 46 37 38 32 36 44 41 36 34 46 41 32 34 45 39 38 38 30 32 34 42 43 35 42 37 31 45 30 38 39 33 45 31 32 33 34 35 36 37 38 42 33 22 2C 22 72 73 73 69 22 3A 2D 38 31 7D 2C 7B 22 6D 61 63 22 3A 22 44 38 34 36 39 34 31 39 31 30 34 36 22 2C 22 64 61 74 61 22 3A 22 30 32 30 31 30 34 31 42 46 46 34 43 30 30 30 32 31 35 45 42 45 46 44 30 38 33 37 30 41 32 34 37 43 38 39 38 33 37 45 37 42 35 36 33 34 44 46 35 32 34 31 31 31 31 30 30 30 30 43 35 35 44 22 2C 22 72 73 73 69 22 3A 2D 33 34 7D 2C 7B 22 6D 61 63 22 3A 22 44 39 36 30 41 44 44 38 30 43 33 37 22 2C 22 64 61 74 61 22 3A 22 30 32 30 31 30 34 31 42 46 46 34 43 30 30 30 32 31 35 45 42 45 46 44 30 38 33 37 30 41 32 34 37 43 38 39 38 33 37 45 37 42 35 36 33 34 44 46 35 32 34 44 38 41 44 45 37 30 43 43 42 35 45 22 2C 22 72 73 73 69 22 3A 2D 33 30 7D 2C 7B 22 6D 61 63 22 3A 22 44 35 46 32 46 43 32 36 31 38 39 45 22 2C 22 64 61 74 61 22 3A 22 30 32 30 31 30 35 30 32 30 41 30 30 30 33 30 33 45 37 46 45 30 39 46 46 34 45 41 42 44 35 46 32 46 43 32 36 31 38 39 45 30 35 30 39 34 38 35 37 33 30 33 31 22 2C 22 72 73 73 69 22 3A 2D 37 39 7D 2C 7B 22 6D 61 63 22 3A 22 43 46 39 43 44 44 36 38 46 33 44 46 22 2C 22 64 61 74 61 22 3A 22 30 32 30 31 30 34 31 42 46 46 34 43 30 30 30 32 31 35 45 42 45 46 44 30 38 33 37 30 41 32 34 37 43 38 39 38 33 37 45 37 42 35 36 33 34 44 46 35 32 34 31 31 31 31 45 30 30 30 43 35 35 43 22 2C 22 72 73 73 69 22 3A 2D 33 35 7D 2C 7B 22 6D 61 63 22 3A 22 43 37 34 30 36 42 34 34 36 38 33 30 22 2C 22 64 61 74 61 22 3A 22 30 32 30 31 30 34 31 42 46 46 34 43 30 30 30 32 31 35 45 42 45 46 44 30 38 33 37 30 41 32 34 37 43 38 39 38 33 37 45 37 42 35 36 33 34 44 46 35 32 34 31 31 31 31 45 30 30 30 43 35 35 45 22 2C 22 72 73 73 69 22 3A 2D 34 30 7D 5D 2C 22 61 6C 6C 43 6F 75 6E 74 22 3A 38 7D 0D 0A 0D 0A
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}
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.
@meetmagdalene, looks like you're getting the right answer, JS is just removing the initial three padded zeros:
[000]1001 1000 0000 0010 0111 0000 0000 0