Skip to content

Instantly share code, notes, and snippets.

@faisalman
Last active January 11, 2023 14:43
Show Gist options
  • Save faisalman/4213592 to your computer and use it in GitHub Desktop.
Save faisalman/4213592 to your computer and use it in GitHub Desktop.
Convert From/To Binary/Decimal/Hexadecimal in JavaScript
/**
* Convert From/To Binary/Decimal/Hexadecimal in JavaScript
* https://gist.github.com/faisalman
*
* Copyright 2012-2015, Faisalman <fyzlman@gmail.com>
* 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'
*/
@akaak
Copy link

akaak commented Apr 13, 2015

jsfiddle code to try out the binary to decimal: https://jsfiddle.net/mubfy05w/1/ Thanks.

@patrickliechty
Copy link

How do you convert a decimal number to a hex number. All of your examples convert it to a hex string.

@ClementNerma
Copy link

Thanks a lot guy ! This code was very helpful for me !

@qm3ster
Copy link

qm3ster commented Dec 20, 2015

Is there a reason not to just do it like this?

/*jshint browser:true, devel:true*/
/*jshint -W097*/ //DEVEL
'use strict';
/**
 * Convert From/To Binary/Decimal/Hexadecimal in JavaScript
 * https://gist.github.com/faisalman
 *
 * Copyright 2012-2015, Faisalman <fyzlman@gmail.com>
 * Licensed under The MIT License
 * http://www.opensource.org/licenses/mit-license
 */

var convertBase = function () {

    function convertBase(baseFrom, baseTo) {
        return function (num) {
            return parseInt(num, baseFrom).toString(baseTo);

        };
    }

    // binary to decimal
    convertBase.bin2dec = convertBase(2, 10);

    // binary to hexadecimal
    convertBase.bin2hex = convertBase(2, 16);

    // decimal to binary
    convertBase.dec2bin = convertBase(10, 2);

    // decimal to hexadecimal
    convertBase.dec2hex = convertBase(10, 16);

    // hexadecimal to binary
    convertBase.hex2bin = convertBase(16, 2);

    // hexadecimal to decimal
    convertBase.hex2dec = convertBase(16, 10);

    return convertBase;
}();


console.log(convertBase.bin2dec('111')); // '7'
console.log(convertBase.dec2hex('42')); // '2a'
console.log(convertBase.hex2bin('f8')); // '11111000'
console.log(convertBase.dec2bin('22')); // '10110'

@ilian6806
Copy link

In this way you can use it like this:
ConvertBase('11110').from(2).to(10);
Witch I think it'cool :) Also I think this it's great extension to the Math object. Cheers :)

@meetmagdalene
Copy link

I'm trying this out for a conversion app I am making, but the hex to decimal is not working as I'd expect it to.

I enter: console.log(ConvertBase.hex2bin("13004E00")); // '1001 1000 0000 0010 0111 0000 0000 0'
When I am expecting '0001 0011 0000 0000 0100 1110 0000 0000'.

Any thoughts?

@andresn
Copy link

andresn commented Feb 24, 2016

@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

@vsltech
Copy link

vsltech commented Mar 25, 2016

thanks a lot i modified few things out for my project check the link: http://vslcreations.in/projects/numbersystem.html

@SeanCannon
Copy link

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'

@SeanCannon
Copy link

I turned this into an NPM library if anybody wants to use it : https://www.npmjs.com/package/aybabtu

@sqrtsanta
Copy link

sqrtsanta commented Feb 25, 2017

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'

@briancodes
Copy link

briancodes commented Jun 8, 2017

@Flyr1Q - const convert function syntax - took me some time to get my head around that line!!

@charles-passille-smartnets

@Flyr1Q genius

@jitendra-kr
Copy link

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

@darkworks
Copy link

@jimmylogic you can decode your long hex data through this online tool : https://www.useotools.com/decode-hex

Copy link

ghost commented Jan 27, 2018

Comprehensive code!!!!! Like it.

@SuperOP535
Copy link

ghost

@VikashSaharan1
Copy link

Excellent Code !!!! Like it.

@alauthor
Copy link

Excellent Code !!!

@zukutoke
Copy link

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?

@zukutoke
Copy link

zukutoke commented Apr 25, 2018

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));  

@cakiray
Copy link

cakiray commented Aug 2, 2018

Is your code just for unsigned numbers?
What should be done if we have signed hexadecimal numbers?

@shantoislam6
Copy link

<!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

@shantoislam6
Copy link

<!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; i

@shantoislam6
Copy link

I can't send my source code as comment here. check out my this repository
https://github.com/shantoislam6/binaryToDec-Ascii-To-String

@idurant
Copy link

idurant commented Jan 15, 2019

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‬

@jtrent238
Copy link

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}

@Aravin
Copy link

Aravin commented Aug 28, 2020

To convert large binary to hex use this code

https://gist.github.com/Aravin/011f17528f90be6bb4f5a4cb253647d6

@jadsongmatos
Copy link

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));

@EBugYT
Copy link

EBugYT commented Dec 18, 2022

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