---
layout: page
title: "JavaScript bindec function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/bindec:362
- /functions/view/bindec
- /functions/view/362
- /functions/bindec:362
- /functions/362
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's bindec

{% codeblock math/bindec.js lang:js https://raw.github.com/kvz/phpjs/master/functions/math/bindec.js raw on github %}
function bindec (binary_string) {
  // From: http://phpjs.org/functions
  // +   original by: Philippe Baumann
  // *     example 1: bindec('110011');
  // *     returns 1: 51
  // *     example 2: bindec('000110011');
  // *     returns 2: 51
  // *     example 3: bindec('111');
  // *     returns 3: 7
  binary_string = (binary_string + '').replace(/[^01]/gi, '');
  return parseInt(binary_string, 2);
}
{% endcodeblock %}

 - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/math/bindec.js)

Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are 
the closest match to this hashtable-like data structure. 

Please also note that php.js offers community built functions and goes by the 
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online 
functions that are far from perfect, in the hopes to spark better contributions. 
Do you have one? Then please just: 

 - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/math/bindec.js)

### Example 1
This code
{% codeblock lang:js example %}
bindec('110011');
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
51
{% endcodeblock %}

### Example 2
This code
{% codeblock lang:js example %}
bindec('000110011');
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
51
{% endcodeblock %}

### Example 3
This code
{% codeblock lang:js example %}
bindec('111');
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
7
{% endcodeblock %}


### Other PHP functions in the math extension
{% render_partial _includes/custom/math.html %}
