smerny Posted September 29, 2009 Share Posted September 29, 2009 lets say i have the binary 1011101.1011 how could i take that, and print this: 001 011 101 . 101 100 notice it adds 0s on the ends if it needs to and leaves the decimal alone Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 29, 2009 Share Posted September 29, 2009 <?php $num="1011101.1011"; $test=wordwrap($num,3,"\n", true); echo $test; ?> http://uk.php.net/manual/en/function.wordwrap.php Quote Link to comment Share on other sites More sharing options...
smerny Posted September 29, 2009 Author Share Posted September 29, 2009 that doesn't solve the problem with adding 0's on the ends and it does not work with the decimal points Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 29, 2009 Share Posted September 29, 2009 what you on about ...... do you want the last two 11 into 00 Quote Link to comment Share on other sites More sharing options...
smerny Posted September 29, 2009 Author Share Posted September 29, 2009 example input: 1011101.1011 example output: 001 011 101 . 101 100 Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 29, 2009 Share Posted September 29, 2009 HOW ARE YOU DETERMINING THAT A 00 IS NEEDED FOR THE END...... sorry my magic crystal ball broke... Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 29, 2009 Share Posted September 29, 2009 example.... if the format of the number has 11 at the end add two 00 , or if there 101 first three numbers add the two 00 at the end, tel us then Quote Link to comment Share on other sites More sharing options...
smerny Posted September 29, 2009 Author Share Posted September 29, 2009 groups of 3 digits... if it does not amount to 3 digits at the ends, add 0's to make it that way (working away from the decimal point) so 1 011 101 . 101 1 becomes 001 011 101 . 101 100 have you ever converted binary to octal by hand? this is the first step Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 29, 2009 Share Posted September 29, 2009 This is what I came up with <?php $test="1011101.1011"; for($i=0;$i<strlen($test);$i++) { $testa[] = $test[$i]; } $test = ""; $i = 1; foreach($testa as $k=>$v) { if($v == ".") { $test .= " . "; $i = 1; continue; } else { $test .= $v; } if($i == 3) { $i = 0; $test .= " "; } $i++; } $test = split(" ", $test); foreach($test as $k=>$v) { if($v == ".") { continue; } while(strlen($test[$k])<3) { $test[$k] .= "0"; } } $test = join(" ", $test); print_r($test); ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 29, 2009 Share Posted September 29, 2009 i was rude deleted(( taking the pain like a man)) Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 29, 2009 Share Posted September 29, 2009 Redarrow, must you be so rude? He posted what he wanted in the first post "notice it adds 0s on the ends if it needs to and leaves the decimal alone"; The time in which you speant being rude I came up with actual working code, quite unlike you. You might want to try some courtesy, or GTFO Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 29, 2009 Share Posted September 29, 2009 if the format changes will it still work? i was not being rude i was simple asking for the format off the numbers... if the numbers are in a special format permanently then it a easy solution? if the numbers continue to change and the format needs to change then it not so easy to get the correct result. example if the binary number get smaller your code does nothink... Quote Link to comment Share on other sites More sharing options...
smerny Posted September 29, 2009 Author Share Posted September 29, 2009 yea red didn't make much sense asking for examples when i gave examples... garethp, at first i thought yours was working, but i got this: input: 11010111.1011 output: 110 101 110 . 101 100 it should be instead: 011 010 111 . 101 100 Quote Link to comment Share on other sites More sharing options...
smerny Posted September 29, 2009 Author Share Posted September 29, 2009 another example of output from your code garethp input: 1101.1011 output: 110 100 . 101 100 i think i see what it's doing, it is adding the 0's to the wrong side of the digits to the left of the decimal... working well on the right side of the decimal though Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 29, 2009 Share Posted September 29, 2009 Redarrow, must you be so rude? He posted what he wanted in the first post "notice it adds 0s on the ends if it needs to and leaves the decimal alone"; The time in which you speant being rude I came up with actual working code, quite unlike you. You might want to try some courtesy, or GTFO thort it was working? WHALE WERE ALL GETTING FORMATS I WONDER WHY Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 29, 2009 Share Posted September 29, 2009 Could you explain why it would be that? If you take your input string, and put a space every third letter, it goes like this 110 10111.1011 110 101 11.1011 110 101 11 . 1011 110 101 11 . 101 1 And then you put a 0 at the end of the groups that are less than three 110 101 110 . 101 100 And you show us how it should work, how the code should reach it's output? Quote Link to comment Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 Hm, I came up with something myself.. Edit: Fixed it. <?php $str = '1011101.1011'; $split = str_split($str); $num_len = count($split) - cc('.', $split); $num_before = array_search('.', $split); $num_after = $num_len - $num_before; if($num_before % 3 != 0) $str = implode(array_fill(0, 3 - $num_before % 3, 0)) . $str; if($num_after % 3 != 0) $str .= implode(array_fill(0, 3 - $num_after % 3, 0)); $split = str_split($str); for($i = 0;$i < count($split);$i++) if($split[$i] == '.') $split[$i] = ' . '; else if($i % 4 == 0) array_splice($split, $i, 0, ' '); echo implode($split); function cc($char, $arr) { $num = 0; foreach($arr as $part) if($part == $char) $num++; return $num; } ?> Quote Link to comment Share on other sites More sharing options...
smerny Posted September 29, 2009 Author Share Posted September 29, 2009 I don't know how the code should reach the output or I wouldn't be here. but do you know binary numbers? 001 and 1 are the same thing, 100 and 1 are not 1.1 and 001 . 100 are the same 100 . 100 are not splitting it into sets of 3 allows it to be more easily seen as octals is all Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 29, 2009 Share Posted September 29, 2009 Here <?php $test="1011101.1011"; while(!str_replace(".", "", $test) % 3) { $test = "0" . $test; } for($i=0;$i<strlen($test);$i++) { $testa[] = $test[$i]; } $test = ""; $i = 1; foreach($testa as $k=>$v) { if($v == ".") { $test .= " . "; $i = 1; continue; } else { $test .= $v; } if($i == 3) { $i = 0; $test .= " "; } $i++; } $test = split(" ", $test); foreach($test as $k=>$v) { if($v == ".") { continue; } while(strlen($test[$k])<3) { $test[$k] .= "0"; } } $test = join(" ", $test); print_r($test); ?> Also, redarrow, only post if you have something useful to contribute Quote Link to comment Share on other sites More sharing options...
smerny Posted September 29, 2009 Author Share Posted September 29, 2009 Thanks guys, got it working now Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 29, 2009 Share Posted September 29, 2009 alexWD fixed it and works perfect... Quote Link to comment Share on other sites More sharing options...
smerny Posted September 29, 2009 Author Share Posted September 29, 2009 yes I used alexWD's example and it put the 0's in the right spots and everything... i just had to tweak it here: foreach($arr as $part) if($part == $char) $num++; into: foreach($arr as $part) { if($part == $char) $num++; } garethp had a good idea also and was on the right track, but i just kept getting 0's added directly to the left of the decimal instead of way at the left end Quote Link to comment Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 yes I used alexWD's example and it put the 0's in the right spots and everything... i just had to tweak it here: foreach($arr as $part) if($part == $char) $num++; into: foreach($arr as $part) { if($part == $char) $num++; } garethp had a good idea also and was on the right track, but i just kept getting 0's added directly to the left of the decimal instead of way at the left end That shouldn't cause a difference because there's only 1 expression being evaluated there. Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 29, 2009 Share Posted September 29, 2009 alexWD code was well hard to understand. but i got there in the end, what i done is shine it on my projector and walked with my wight stick and pointed at the huge code until i understood it.... maybe in 10 years time ill code like that.... Quote Link to comment Share on other sites More sharing options...
Alex Posted September 29, 2009 Share Posted September 29, 2009 I'm about to head off to bed in a little bit (After I finish working on something..) But if you want I'll break it down and explain it part by part. Quote Link to comment 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.