phpjs
Version:
54 lines (44 loc) • 1.78 kB
Markdown
layout: page
title: "JavaScript base_convert function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/base_convert:359
- /functions/view/base_convert
- /functions/view/359
- /functions/base_convert:359
- /functions/359
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's base_convert
{% codeblock math/base_convert.js lang:js https://raw.github.com/kvz/phpjs/master/functions/math/base_convert.js raw on github %}
function base_convert (number, frombase, tobase) {
// From: http://phpjs.org/functions
// + original by: Philippe Baumann
// + improved by: Rafał Kukawski (http://blog.kukawski.pl)
// * example 1: base_convert('A37334', 16, 2);
// * returns 1: '101000110111001100110100'
return parseInt(number + '', frombase | 0).toString(tobase | 0);
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/math/base_convert.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/base_convert.js)
### Example 1
This code
{% codeblock lang:js example %}
base_convert('A37334', 16, 2);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'101000110111001100110100'
{% endcodeblock %}
### Other PHP functions in the math extension
{% render_partial _includes/custom/math.html %}