decbin

(PHP 4, PHP 5, PHP 7, PHP 8)

decbinConvertit de décimal en binaire

Description

decbin(int $num): string

Retourne une chaîne contenant la représentation binaire de l'entier num donné en argument.

Liste de paramètres

num

Valeur décimale à convertir

Interval d'entrée sur des machines 32-bit
Paramètre num positif Paramètre num négatif Valeur retournée
0   0
1   1
2   10
... progression normale ...
2147483646   1111111111111111111111111111110
2147483647 (plus grand entier signé)   1111111111111111111111111111111 (31 uns)
2147483648 -2147483648 10000000000000000000000000000000
... progression normale ...
4294967294 -2 11111111111111111111111111111110
4294967295 (plus grand entier non-signé) -1 11111111111111111111111111111111 (32 uns)
Interval d'entrée sur des machines 64-bit
Paramètre num positif Paramètre num négatif Valeur retournée
0   0
1   1
2   10
... progression normale ...
9223372036854775806   111111111111111111111111111111111111111111111111111111111111110
9223372036854775807 (plus grand entier signé)   111111111111111111111111111111111111111111111111111111111111111 (63 uns)
  -9223372036854775808 1000000000000000000000000000000000000000000000000000000000000000
... progression normale ...
  -2 1111111111111111111111111111111111111111111111111111111111111110
  -1 1111111111111111111111111111111111111111111111111111111111111111 (64 uns)

Valeurs de retour

Une représentation binaire de num.

Exemples

Exemple #1 Exemple avec decbin()

<?php
echo decbin(12) . "\n";
echo
decbin(26);
?>

L'exemple ci-dessus va afficher :

1100
11010

Voir aussi

  • bindec() - Convertit de binaire en décimal
  • decoct() - Convertit de décimal en octal
  • dechex() - Convertit de décimal en hexadécimal
  • base_convert() - Convertit un nombre entre des bases arbitraires
  • printf() - Affiche une chaîne de caractères formatée, en utilisant %b, %032b ou %064b comme format
  • sprintf() - Retourne une chaîne formatée, en utilisant %b, %032b ou %064b comme format

add a note

User Contributed Notes 2 notes

up
8
rambabusaravanan at gmail dot com
8 years ago
Print as binary format with leading zeros into a variable in one simple statement.

<?php
$binary
= sprintf('%08b', $decimal); // $decimal = 5;
echo $binary; // $binary = "00000101";
?>
up
7
Anonymous
19 years ago
Just an example:
If you convert 26 to bin you'll get 11010, which is 5 chars long. If you need the full 8-bit value use this:

$bin = decbin(26);
$bin = substr("00000000",0,8 - strlen($bin)) . $bin;

This will convert 11010 to 00011010.
To Top