LoginSignup
0
0

More than 3 years have passed since last update.

PHPで十進法->二進法に変換する

Posted at

PHPで十進法->二進法に変換するコードを書いてみました。
decbin()関数があリますが。

binary.php
<?php

  $input = fgets(STDIN);
  $binary = array();

  while(true){
    $remainder = $input % 2;
    $input = floor($input/2);
    $binary[] = $remainder;
    if ($input == 0 || $input == 1){
      $binary[] = $input;
      break;
    }  
  }
  foreach(array_reverse($binary) as $bit){
    echo $bit;
  }
}
?>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0