Use PHP functions in JavaScript

JavaScript dechex

Returns a string containing a hexadecimal representation of the given number

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
20
function dechex (number) {
    // Returns a string containing a hexadecimal representation of the given number  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/dechex    // +   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);
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
dechex(10);

Should return

1
'a'

» Example 2

Running

1
dechex(47);

Should return

1
'2f'

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have dechex goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Kevin van Zonneveld
16 Aug '09 Permalink

q  @ Lokde: Come again?

Gravatar
Lokde
11 Aug '09 Permalink

q  rediffmail javascript:;; error

Gravatar
Brett Zamir
5 Aug '09 Permalink

q  @pilus: I finally got around to updating the other related functions--to my knowledge it seems only the dec* functions needed changing, as these were the ones relying on toString() with a numeric argument (and which could have negative values (i.e., unlike bin2hex))...

Gravatar
Brett Zamir
18 Jun '09 Permalink

q  Ok, I made the credit fix, but I'll hold off on the other functions for now (unless you want to indicate which ones need it, etc.)

Gravatar
pilus
11 Jun '09 Permalink

q  actually i would like people at stackoverflow to be credited, but when i want to make a comment there, they required me to register ... and I don't want to register ....

just so people still credit the original link, here they are :

http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript

@Brett : thx for the credit ... :D, but the other numeric conversion function should do the same, I think, I've tried the decbin in php, it returns non negative number, but I have not tried the one in php.js ... :D

Gravatar
Brett Zamir
10 Jun '09 Permalink

q  Great, thanks, Pilus! I've fixed it in SVN and credited you for it. The only difference was I kept the 2nd argument in parseInt even though its redundant, since jslint complains about it...

Gravatar
pilus
10 Jun '09 Permalink

q   hohoho .... using information from this link : http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript, i managed to modify the function to satisfy my needs, I'm currently porting a AES-PHP algorithm to JS, and I've been pondering my code, because there's some infinite loop there somewhere, it's because the dechex-js returned a negative hex for a negative input, whereas PHP version does not.

So, here you, hopefully it could be usefull for anyone else ... :P

1
2
3
4
56
function dechex(number) {
        if (number < 0) {
           return (0xFFFFFFFF+number+1).toString(16);
        }
    return parseInt(number).toString(16);}

Gravatar
pilus
10 Jun '09 Permalink

q  what about this one here :

dechex(-1415723993)

it returns "-54623bd9", but when tried in php and in MSWIN calc, returns AB9DC427 and FFFFFFFFAB9DC427 consecutively. anybody know what's wrong ? cause i don't ... >.<

Gravatar
Kevin van Zonneveld
6 Oct '08 Permalink

q  @ Philippe Baumann & Onno Marsman: Excellent work gentlemen. As far as the loosely typed discussion, I would have to side with Onno there.
PS, we almost have the entire unported list of the math section covered!

Gravatar
Onno Marsman
5 Oct '08 Permalink

q  It is well known that PHP is a weakly typed language. Of course this is not mentioned at every functions page in the documentation.
And because javascript is also weakly typed I think it would be obvious to make this library also weakly typed. And with that I mean: as close to the behavior of PHP as possible, which, of course, is the main goal of this library.

Gravatar
Philippe Baumann
4 Oct '08 Permalink

q   And while we're already at it:

1
2
3
4
function base_convert(number, frombase, tobase)
{
        return parseInt(number, frombase).toString(tobase);
}

Gravatar
Philippe Baumann
4 Oct '08 Permalink

q  Nevermind. It seems you were right and the PHP functions also accept string-type arguments. However the manual doesn't state it anywhere, so I assume it's not specially encouraged.

Also, I mistyped 'specification' in my previous comment (shame on me).

Gravatar
Philippe Baumann
4 Oct '08 Permalink

q   Well, the specificain is
[CODE="php"]
string dechex ( int number)
[/CODE]
so it really expects an integer and only returns a string.

Gravatar
Onno Marsman
4 Oct '08 Permalink

q   dechex('16') does not work correctly. A fix:

1
2
3
function dechex(number) {    
    return parseInt(number).toString(16);
}

Gravatar
Philippe Baumann
3 Oct '08 Permalink

q   This also applies the other way round:

1
2
3
4
56
7
8
9
1011
function octdec(oct_string)
{
        oct_string = (oct_string+'').replace(/[^0-7]/gi, '');
        return parseInt(oct_string, 8);
} 
function bindec(binary_string)
{
        binary_string = (binary_string+'').replace(/[^01]/gi, '');
        return parseInt(binary_string, 2);}


Note that the PHP manual pages for octdec() and bindec() do not mention the filtering, however it does actually take place:

[CODE="php"]
<?php
echo octdec('a180'); // 'a180' -> '10' -> 8
echo "<br />";
echo bindec('c120'); // 'c120' -> '10' -> 2
?>
[/CODE]

Gravatar
Kevin van Zonneveld
3 Oct '08 Permalink

q  @ Enrique González: Yes it could! Added!

Gravatar
Enrique González
3 Oct '08 Permalink

q   The same code could be used for decoct and decbin functions:

1
2
3
4
56
7
function decoct(number){
  return number.toString(8);
  }
  
function decbin(number){  return number.toString(2);
  }


Contribute a New function

More functions

In this category

abs
acos
acosh
asin
asinh
atan
atan2
atanh
base_convert
bindec
ceil
cos
cosh
decbin
» dechex
decoct
deg2rad
exp
expm1
floor
fmod
getrandmax
hexdec
hypot
is_finite
is_infinite
is_nan
lcg_value
log
log10
log1p
max
min
mt_getrandmax
mt_rand
octdec
pi
pow
rad2deg
rand
round
sin
sinh
sqrt
tan
tanh

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !