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: 1008.1718 // 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); } |
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.
@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))...
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.)
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
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...
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
function dechex(number) {
if (number < 0) {
return (0xFFFFFFFF+number+1).toString(16);
}
return parseInt(number).toString(16);
}
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 ... >.<
@ 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!
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.
And while we're already at it:
[CODE="Javascript"]
function base_convert(number, frombase, tobase)
{
return parseInt(number, frombase).toString(tobase);
}
[/CODE]
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).
Well, the specificain is
[CODE="php"]
string dechex ( int number)
[/CODE]
so it really expects an integer and only returns a string.
dechex('16') does not work correctly. A fix:
[CODE="Javascript"]
function dechex(number) {
return parseInt(number).toString(16);
}
[/CODE]
This also applies the other way round:
[CODE="Javascript"]
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);
}
[/CODE]
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]


Kevin van Zonneveld
16 Aug '09