No cache version.

Caching disabled. Default setting for this page:enabled (code LNG204)
If the display is too slow, you can disable the user mode to view the cached version.

Rechercher une fonction PHP

bindec

(PHP 4, PHP 5, PHP 7)

bindecBinary to decimal

Description

bindec ( string $binary_string ) : number

Returns the decimal equivalent of the binary number represented by the binary_string argument.

bindec() converts a binary number to an integer or, if needed for size reasons, float.

bindec() interprets all binary_string values as unsigned integers. This is because bindec() sees the most significant bit as another order of magnitude rather than as the sign bit.

PHP: bindec - Manual Home of Manuel PHP  Contents Haut

Parameters

binary_string

The binary string to convert

Warning

The parameter must be a string. Using other data types will produce unexpected results.

PHP: bindec - Manual Home of Manuel PHP  Contents Haut

Return Values

The decimal value of binary_string

PHP: bindec - Manual Home of Manuel PHP  Contents Haut

Examples

Example #1 bindec() example

<?php
echo bindec('110011') . "\n";
echo 
bindec('000110011') . "\n";

echo 
bindec('111');
?>

The above example will output:

51
51
7

Example #2 bindec() interprets input as unsigned integers

<?php
/*
 * The lesson from this example is in the output
 * rather than the PHP code itself.
 */

$magnitude_lower pow(2, (PHP_INT_SIZE 8) - 2);
p($magnitude_lower 1);
p($magnitude_lower'See the rollover?  Watch it next time around...');

p(PHP_INT_MAX'PHP_INT_MAX');
p(~PHP_INT_MAX'interpreted to be one more than PHP_INT_MAX');

if (
PHP_INT_SIZE == 4) {
    
$note 'interpreted to be the largest unsigned integer';
} else {
    
$note 'interpreted to be the largest unsigned integer
              (18446744073709551615) but skewed by float precision'
;
}
p(-1$note);


function 
p($input$note '') {
    echo 
"input:        $input\n";

    
$format '%0' . (PHP_INT_SIZE 8) . 'b';
    
$bin sprintf($format$input);
    echo 
"binary:       $bin\n";

    
ini_set('precision'20);  // For readability on 64 bit boxes.
    
$dec bindec($bin);
    echo 
'bindec():     ' $dec "\n";

    if (
$note) {
        echo 
"NOTE:         $note\n";
    }

    echo 
"\n";
}
?>

Output of the above example on 32 bit machines:

input:        1073741823
binary:       00111111111111111111111111111111
bindec():     1073741823

input:        1073741824
binary:       01000000000000000000000000000000
bindec():     1073741824
NOTE:         See the rollover?  Watch it next time around...

input:        2147483647
binary:       01111111111111111111111111111111
bindec():     2147483647
NOTE:         PHP_INT_MAX

input:        -2147483648
binary:       10000000000000000000000000000000
bindec():     2147483648
NOTE:         interpreted to be one more than PHP_INT_MAX

input:        -1
binary:       11111111111111111111111111111111
bindec():     4294967295
NOTE:         interpreted to be the largest unsigned integer

Output of the above example on 64 bit machines:

input:        4611686018427387903
binary:       0011111111111111111111111111111111111111111111111111111111111111
bindec():     4611686018427387903

input:        4611686018427387904
binary:       0100000000000000000000000000000000000000000000000000000000000000
bindec():     4611686018427387904
NOTE:         See the rollover?  Watch it next time around...

input:        9223372036854775807
binary:       0111111111111111111111111111111111111111111111111111111111111111
bindec():     9223372036854775807
NOTE:         PHP_INT_MAX

input:        -9223372036854775808
binary:       1000000000000000000000000000000000000000000000000000000000000000
bindec():     9223372036854775808
NOTE:         interpreted to be one more than PHP_INT_MAX

input:        -1
binary:       1111111111111111111111111111111111111111111111111111111111111111
bindec():     18446744073709551616
NOTE:         interpreted to be the largest unsigned integer
              (18446744073709551615) but skewed by float precision

PHP: bindec - Manual Home of Manuel PHP  Contents Haut

Notes

Note:

The function can convert numbers that are too large to fit into the platforms integer type, larger values are returned as float in that case.

PHP: bindec - Manual Home of Manuel PHP  Contents Haut

See Also

Find a PHP function

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-function.bindec.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:fr Manuel PHP : http://php.net

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut