donglu5728 2014-06-21 09:27
浏览 27

在php中将septet转换为八位字节

I need to convert septets to octest like this c example:

private void septetToOctet()
{
    int len = 1;
    int j = 0;
    int septetcount = septet.Count;

    while (j < septet.Count - 1)
    {
        string tmp = septet[j]; // storing jth value
        string tmp1 = septet[j + 1]; //storing j+1 th value
        string mid = SWAP(tmp1);
        tmp1 = mid;
        tmp1 = tmp1.Substring(0, len);
        string add = SWAP(tmp1);
        tmp = add + tmp;// +"-";
        tmp = tmp.Substring(0, 8);
        //txtoctet.Text += tmp + " || ";
        octet.Add(tmp);
        len++;
        if (len == 8)
        {
            len = 1;
            j = j + 1;
        }
        j = j + 1;
    }
}

..only problem is - i need to do it in php. Does anybody have an code example for this ?

  • 写回答

1条回答 默认 最新

  • doulingzhuang3079 2014-06-21 10:06
    关注

    Most of the code can be translated to PHP with little changes. For instance, the first two assignments would be just $len = 1 and $j = 0. There is no strong static typing in PHP.

    Since numbers and strings are scalars in PHP, you cannot call methods on them, so you cannot do tmp1.substring but would have to do substr($tmp1, …).

    septet and octet would be arrays in PHP. Again, there is no methods on arrays in PHP. To add to an array, you simply do $octet[] = $tmp. To count the elements you do count($septet).

    There is no SWAP in PHP, but you can derive the rather trivial function from Is there a built in swap function in C?

    These hints should get you closer to a working implementation.

    评论

报告相同问题?