note 114186 added to function.decbin

From: Date: Wed, 22 Jan 2014 09:07:01 +0000
Subject: note 114186 added to function.decbin
Groups: php.notes 
Request: Send a blank email to php-notes+get-198299@lists.php.net to get a copy of this message
hi folks, i struggled for a day to get a big decimal number converted into binary, on the windows platform. finally with bcmath functions this is what worked for me. function bc_convert2bin($string) { //got it to work with bcmath functions, works for 64 bit on 32 bit windows machine $finished=0; $base=2; $bin_nr=''; if(preg_match("/[^0-9]/", $string)) { for($i=0; $string!=chr($i); $i++) { $dec_nr=$i; } } else { $dec_nr=$string; } //while( $dec_nr>$base ) { while( bccomp($dec_nr,$base) == 1 ) { //$base=$base*2; $base=bcmul($base,'2'); //if($base>$dec_nr) { if( bccomp($base,$dec_nr) == 1 ) { //$base=$base/2; $base=bcdiv($base,'2'); break; } } while(!$finished) { //if(($dec_nr-$base)>0) { if( bccomp( bcsub($dec_nr,$base) , 0) == 1 ) { //$dec_nr=$dec_nr-$base; $dec_nr=bcsub($dec_nr,$base); $bin_nr.=1; //$base=$base/2; $base=bcdiv($base,'2'); //} elseif(($dec_nr-$base)<0) { } elseif( bccomp( bcsub($dec_nr,$base) , 0) == -1 ) { $bin_nr.=0; //$base=$base/2; $base=bcdiv($base,'2'); //} elseif(($dec_nr-$base)==0) { } elseif( bccomp( bcsub($dec_nr,$base) , 0) == 0 ) { $bin_nr.=1; $finished=1; //while($base>1) { while( bccomp($base,1) == 1 ) { $bin_nr.=0; //$base=$base/2; $base=bcdiv($base,'2'); } } } return $bin_nr; } ---- Server IP: 72.52.91.14 Probable Submitter: 72.52.91.14 ---- Manual Page -- http://php.net/manual/en/function.decbin.php Edit -- https://master.php.net/note/edit/114186 Del: integrated -- https://master.php.net/note/delete/114186/integrated Del: useless -- https://master.php.net/note/delete/114186/useless Del: bad code -- https://master.php.net/note/delete/114186/bad+code Del: spam -- https://master.php.net/note/delete/114186/spam Del: non-english -- https://master.php.net/note/delete/114186/non-english Del: in docs -- https://master.php.net/note/delete/114186/in+docs Del: other reasons-- https://master.php.net/note/delete/114186 Reject -- https://master.php.net/note/reject/114186 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198299) next »