将二进制数组转换为十进制字符串奇怪行为

时间:2014-06-21 22:08:29

标签: php c arrays memory int

我目前在php中使用int[]数组实现左移,需要在操作后返回小数。所以我编写了以下代码片段来尝试将二进制数组转换为十进制数。

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

现在奇怪的问题出在循环中$cr != 0$i返回一个令人难以置信的值已经不能满足循环条件,但我无法弄清楚为什么会发生这种情况。这是相关代码的其余部分。

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

我还实施了working version on ideone for testing。有谁知道为什么会这样?

感谢。

2 个答案:

答案 0 :(得分:0)

好的,显然问题增加了超过需要的数量,并且在bin2dec中不必要地从长度中减去1。这是最终的工作版本:

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

答案 1 :(得分:-1)

使用字符串......

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