Converting binary array to decimal string strange behaviour

青春壹個敷衍的年華 提交于 2020-01-15 12:47:10

问题


I'm currently implement left shift using int[] arrays in php and need to get back the decimal after operation. So I have written the following snippet to attempt conversion of binary array to decimal.

function bin2dec($bin)
{
    $length = count($bin) - 1;
    $sum = 0;
    //convert using doubling
    for($i = 0; $i < $length; $i++)
    {
        //use string_add if doubling bigger than int32
        if($i >= 16)
        {
            $double = $this->string_add("$sum", "$sum");
            $cr = $bin[$i];
            if($cr == 0)
            {
                $sum = $this->string_add($sum, $double);
            }
            else{
                return $i;//WHAT's UP??!
                $add = $this->string_add($double, "$cr");
                $sum = $this->string_add($sum, $add);
            }
        }
        else{
            $sum += ($sum * 2) + $bin[$i];
        }
    }
    return $sum;
}

Now the weird problem is in the loop where $cr != 0, $i returns an unbelievable value already not satisfying the loop condition but I can't figure out why this is happening. Here's the rest of the relevant code.

function string_add($a, $b)
{
    $lena = strlen($a); $lenb = strlen($b);
    if($lena == $lenb)
    {
        $len = $lena - 1;//any
    }
    else if($lena > $lenb)
    {
        $b = str_pad($b, $lena, "0", STR_PAD_LEFT);
        $len = $lena - 1;
    }
    else if($lenb > $lena){
        $a = str_pad($a, $lenb, "0", STR_PAD_RIGHT);
        $len = $lenb - 1;
    }
    $result = "";
    for ($i = $len, $carry = 0; $i >= 0 || $carry != 0; $i--)
    {
        $add1 = $i < 0 ? 0 : $a[$i];
        $add2 = $i < 0 ? 0 : $b[$i];
        $add = $add1 + $add2 + $carry;
        if ($add > 9) {
            $carry = 1;
            $add -= 10;
        }
        else {
            $carry = 0;
        }
        $result .= $add;
    }
    return strrev($result);
}

$arr = array_pad(array(1), 62, 0);
$dec = bin2dec($arr);
return $dec;//test

I have also implemented a working version on ideone for testing. Does anyone understand why this is happening?

Thanks.


回答1:


Ok, so apparently the problem was adding more than needed and unnecessarily subtracting 1 from length in bin2dec. Here's the final working version:

<?php
class MyClass{
    function bin2dec($bin)
    {
        $length = count($bin);
        $sum = 0;
        //convert using doubling
        for($i = 0; $i < $length; $i++)
        {
            //use string_add if doubling bigger than int32
            if($i >= 16)
            {
                $sum = $this->string_add("$sum", "$sum");
                $cr = $bin[$i];
                if($cr != 0){
                    $sum = $this->string_add($sum, "$cr");
                }
            }
            else{
                $sum += $sum + $bin[$i];
            }
        }
        return $sum;
    }

    function string_add($a, $b)
    {
        $lena = strlen($a); $lenb = strlen($b);
        if($lena == $lenb)
        {
            $len = $lena - 1;//any
        }
        else if($lena > $lenb)
        {
            $b = str_pad($b, $lena, "0", STR_PAD_LEFT);
            $len = $lena - 1;
        }
        else if($lenb > $lena){
            $a = str_pad($a, $lenb, "0", STR_PAD_RIGHT);
            $len = $lenb - 1;
        }
        $result = "";
        for ($i = $len, $carry = 0; $i >= 0 || $carry != 0; $i--)
        {
            $add1 = $i < 0 ? 0 : $a[$i];
            $add2 = $i < 0 ? 0 : $b[$i];
            $add = $add1 + $add2 + $carry;
            if ($add > 9) {
                $carry = 1;
                $add -= 10;
            }
            else {
                $carry = 0;
            }
            $result .= $add;
        }
        return strrev($result);
    }
}

$man = new MyClass();
$arr = array_pad(array(1), 62, 0);
$dec = $man->bin2dec($arr);
echo $dec;



回答2:


Using strings...

function bin2dec($bin)
{
    $length = count($bin);
    $sum = "";
    for ( $i = 0; $i < $length; $i++)
    {
        $sum = $sum . ( $bin[$i} == 0 ? '0' : '1');
    }
    return $sum;
}

function string_add($a, $b)
{
    // not efficient, but obvious
    $maxlen = ( strlen($a) > strlen($b) ? strlen($a) : strlen($b);

    // make them both same length as longest
    $a = str_pad($a, $maxlen, "0", STR_PAD_LEFT);
    $b = str_pad($b, $maxlen, "0", STR_PAD_LEFT);
    $result = "";
    $carry = 0;

    // start from the right end
    for ($i = $maxlen - 1; $i >= 0; $i--)
    {
        $val = $a[$i] + $b[$i] + $carry;
        $result = ( $val % 10 ) . $result;
        $carry = $val / 10;
    }

    // handle final carry if present
    if ( $carry > 0 )
    {
        $result = $carry . $result;
    }

    return $result;
}

$arr = array_pad(array(1), 62, 0);
$dec = bin2dec($arr);
return $dec;//test


来源:https://stackoverflow.com/questions/24346286/converting-binary-array-to-decimal-string-strange-behaviour

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!