Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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'
*/
@carloscarcamo

This comment has been minimized.

Copy link

commented Oct 25, 2013

excelent code!!!

@ueg1990

This comment has been minimized.

Copy link

commented Feb 10, 2014

hello, im trying to convert a 60 bit binary string to decimal in javascript but i am not getting the correct output. For example converting "001000110111100101101101111011100111000010110101001010000000" gives me 159762195898126980 INSTEAD OF 159762195898126976. Is there a way to get around this issue? Would really appreciate the help :)

@centurianii

This comment has been minimized.

Copy link

commented Mar 6, 2014

returnt this; a.k.a. return window;(???)
@ueg1990 : don't work on more than 32 bits or look for BigInteger js libraries!

@bergus

This comment has been minimized.

Copy link

commented Jul 13, 2014

@centurianii: Up to 52 bits work well when stored in floats. Only bitwise operations (not what we're doing here) are limited to 32 bits.
@faisalman: convertBase is written as a constructor, then you should use it as one. You're leaking properties currently.

@Nate-Wilkins

This comment has been minimized.

Copy link

commented Feb 1, 2015

@faisalman setting this methods on the convertBase function is a really bad idea - it's currently assigning the from and to sub methods onto whatever the current convertBase context is. Even if it wasn't global I still wouldn't consider that a good idea.

@akaak

This comment has been minimized.

Copy link

commented Apr 13, 2015

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

@patrickliechty

This comment has been minimized.

Copy link

commented Apr 14, 2015

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

@ClementNerma

This comment has been minimized.

Copy link

commented Dec 5, 2015

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

@qm3ster

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Feb 7, 2016

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

This comment has been minimized.

Copy link

commented Feb 17, 2016

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Feb 21, 2017

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

This comment has been minimized.

Copy link

commented Feb 22, 2017

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

@Flyr1Q

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Jun 8, 2017

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

@charles-passille-smartnets

This comment has been minimized.

Copy link

commented Sep 25, 2017

@Flyr1Q genius

@jitendra-kr

This comment has been minimized.

Copy link

commented Nov 21, 2017

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

This comment has been minimized.

Copy link

commented Dec 21, 2017

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

@ghost

This comment has been minimized.

Copy link

commented Jan 27, 2018

Comprehensive code!!!!! Like it.

@SuperOP535

This comment has been minimized.

Copy link

commented Feb 2, 2018

ghost

@VikashSaharan1

This comment has been minimized.

Copy link

commented Apr 2, 2018

Excellent Code !!!! Like it.

@Mr-Everything

This comment has been minimized.

Copy link

commented Apr 18, 2018

Excellent Code !!!

@zukutoke

This comment has been minimized.

Copy link

commented Apr 24, 2018

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Aug 2, 2018

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

@shantoislam6

This comment has been minimized.

Copy link

commented Nov 2, 2018

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

This comment has been minimized.

Copy link

commented Nov 2, 2018

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

This comment has been minimized.

Copy link

commented Nov 2, 2018

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

@idurant

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Oct 16, 2019

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}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.