Use PHP functions in JavaScript

JavaScript decbin

Returns a string containing a binary representation of the number

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
function decbin (number) {
    // Returns a string containing a binary representation of the number  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/decbin    // +   original by: Enrique Gonzalez
    // +   bugfixed by: Onno Marsman
    // +   improved by: http://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript
    // +   input by: pilus
    // +   input by: nord_ua    // *     example 1: decbin(12);
    // *     returns 1: '1100'
    // *     example 2: decbin(26);
    // *     returns 2: '11010'
    // *     example 3: decbin('26');    // *     returns 3: '11010'
    if (number < 0) {
        number = 0xFFFFFFFF + number + 1;
    }
    return parseInt(number, 10).toString(2);}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
decbin(12);

Should return

1
'1100'

» Example 2

Running

1
decbin(26);

Should return

1
'11010'

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 decbin 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  @ Tim de Koning: Thanks for pointing that out.

Gravatar
Tim de Koning
6 Aug '09 Permalink

q   Please note that there is a difference between the handling of large numbers in different browsers. e.g.

IE8

1
2
>>decbin(3747030078639374300)
"1.101000000000000100000000000010000000000000001(e+61)"


Firefox 3.5
1
2
>>> decbin(3747030078639374300)
"11010000000000001000000000000100000000000000010000000000000000"


This is caused by the fact that Number.toString(2) doesn't work in IE for numbers written in scientific notation internally.

Gravatar
Brett Zamir
5 Aug '09 Permalink

q  @nord_ua: Thanks for the report! Fixed in SVN...

Gravatar
nord_ua
4 Aug '09 Permalink

q  

1
decbin(-3)

-11

Gravatar
ben
6 Nov '08 Permalink

q  wow!!! so cool!!!

Gravatar
Onno Marsman
4 Oct '08 Permalink

q   decbin('8') does not work correctly. A fix:

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


Contribute a New function

Download

Download

There is a wide variety of packages if the default doesn't suit you.
You can also compile your own package to avoid any overhead.

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 !

RSS

Tweets

Comments

Who uses php.js

If you use php.js, let us know and get linked.

Progress

php.js is complete for 83.7%

php.js on

Discuss php.js' future at Google Groups
Help improve php.js on github



Powered by php.js
Stats