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

{% codeblock math/dechex.js lang:js https://raw.github.com/kvz/phpjs/master/functions/math/dechex.js raw on github %}
function dechex (number) {
  // From: http://phpjs.org/functions
  // +   original by: Philippe Baumann
  // +   bugfixed by: Onno Marsman
  // +   improved by: http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript
  // +   input by: pilus
  // *     example 1: dechex(10);
  // *     returns 1: 'a'
  // *     example 2: dechex(47);
  // *     returns 2: '2f'
  // *     example 3: dechex(-1415723993);
  // *     returns 3: 'ab9dc427'
  if (number < 0) {
    number = 0xFFFFFFFF + number + 1;
  }
  return parseInt(number, 10).toString(16);
}
{% endcodeblock %}

 - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/math/dechex.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/dechex.js)

### Example 1
This code
{% codeblock lang:js example %}
dechex(10);
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
'a'
{% endcodeblock %}

### Example 2
This code
{% codeblock lang:js example %}
dechex(47);
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
'2f'
{% endcodeblock %}

### Example 3
This code
{% codeblock lang:js example %}
dechex(-1415723993);
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
'ab9dc427'
{% endcodeblock %}


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