Wouser Posted August 26, 2006 Share Posted August 26, 2006 Well Guys... I better hope ya good at PHP otherwise I registrated here for nothing lol :DWell now ontopic again...[b]Example:[/b]I have the number 7 (or 10 or 12 or 123 etc etc) $number = 7;Now I want that the 7 will get splitted in 3 other numbers and on bineary way...So that would be2^0 = 12^1 = 22^2 = 41+2+4 = 7So the output should be 1, 2, 4Now I got this script:[code]<?php$decimaal = 7;$binair = decbin($decimaal);$stukjes = str_split($binair);// str_split split vanaf links, maar we rekenen vanaf rechtsarray_reverse($stukjes);$delen = array();foreach ($stukjes as $key => $stukje) { if ($stukje == 1) { $delen[] = pow(2, $key); }}print_r($delen);/** OUTPUT: * Array * ( * [0] => 1 * [1] => 2 * [2] => 4 * )**/?>[/code]What is the problem with this:Well for example I do $decimaal = 10;The output will be not:[code]Array( [1] => 2 [3] => 8)[/code]but is[code]Array( [0] => 1 [1] => 4)[/code]other arrays must be equal to 0 then becaus the binary code would be for 10 in this example 0101Now how should I do this :)PS: if ya got a other code or something that likes on this sh1t i mean then also feel free to post Quote Link to comment https://forums.phpfreaks.com/topic/18758-making-bineary-code/ Share on other sites More sharing options...
kenrbnsn Posted August 26, 2006 Share Posted August 26, 2006 Here's one way to do what I think you're asking:[code]<?php$bin = sprintf("%b",100);for ($i=0;$i<strlen($bin);$i++) $binary[$i] = $bin{$i};$revbin = array_reverse($binary);$tmp = array();for($i=0;$i<count($revbin);$i++) { $x = pow(2,$i)*$revbin[$i]; if ($x != 0) $tmp[] = $x; }echo implode(',',$tmp);?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/18758-making-bineary-code/#findComment-80926 Share on other sites More sharing options...
ronverdonk Posted August 26, 2006 Share Posted August 26, 2006 Here's another one:[code]<?php// works up to dec64K-1, change for bigger numbersecho convit(64032);function convit($count) {$bin = decbin($count);$bin = substr("0000000000000000",0,16 - strlen($bin)) . $bin;echo $bin . ' = ';for ($i=0,$j=15; $i<16; $i++,$j--) if (substr($bin, $j, 1) == '1') echo pow(2, $i). ', ';}?> [/code]Ronald 8) Quote Link to comment https://forums.phpfreaks.com/topic/18758-making-bineary-code/#findComment-80927 Share on other sites More sharing options...
kenrbnsn Posted August 27, 2006 Share Posted August 27, 2006 You end of with a trialing comma with your loop. I would use a temporary array and implod it:[code]<?php$tmp = array()for ($i=0,$j=15; $i<16; $i++,$j--) if (substr($bin, $j, 1) == '1') $tmp[] = echo pow(2, $i);echo implode(', ',$tmp);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18758-making-bineary-code/#findComment-81010 Share on other sites More sharing options...
sasa Posted August 27, 2006 Share Posted August 27, 2006 or[code]<?phpfunction i2b ($a){ $i = -1; while (($x = pow(2,++$i)) <= $a) if ($a & $x) $out[$i] = $x; return $out;}$out = i2b(55);echo array_sum($out),' = ',implode(' + ',$out);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18758-making-bineary-code/#findComment-81028 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.