Jump to content

Recommended Posts

  • Replies 52
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

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

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);
?>

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

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...

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

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

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?

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;
}
?>

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

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

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

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.

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....

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.