public static byte[] OS2IP (byte[] x) 
{
int i = 0; while ((x [i++] == 0x00) && (i < x.Length)) 
{
// confuse compiler into reporting a warning with {}
} i--; if (i > 0) 
{
byte[] result = new byte [x.Length - i];
Buffer.BlockCopy (x, i, result, 0, result.Length);
return result;
}
else
return x;
}
用 VC6编写,实在时间来不及。200分先答先给分. 再开贴给100

解决方案 »

  1.   

    BYTE* OS2IP(BYTE* x, int nLen)
    {
    int i = 0;
    while ((x[i++] == 0x00) && (i <nLen))
    {
    // confuse compiler into reporting a warning with {}
    }
    i--; if (i > 0)
    {
    BYTE* pResult = new BYTE[nLen-i];
    memcpy(pResult, x+i, nLen);
    return pResult;
    }
    else
    return x;
    }
      

  2.   

    否则就要用标准库的 vector 类了,但用vector不象是你要的答案
      

  3.   

    请看 perl版本sub os2ip {
        my $string = shift;
        my $base = PARI(256);
        my $result = 0;
        my $l = length($string); 
        for (0 .. $l-1) {
            my ($c) = unpack "x$_ a", $string;
            my $a = int(ord($c));
            my $val = int($l-$_-1); 
            $result += $a * ($base**$val);
        }
        return $result;
    }看来C#地实现达不到我的目的。
    不过仍然200分给出。
      

  4.   

    SoLike(思危) 的答案基本可用,不过,如果确实新分配了数组,为了能够返回新数组的长度,建议把 int nLen 参数改为 int& nLen,并在新分配数组之后将此值更新。另外,此函数的调用者则有义务检查返回的 BYTE* 结果是否是传入的原始数组,如果不是应该将原数组释放,并查看于 nLen 中返回的新长度。当然,如果不追求复制时的效率的话,使用一个 vector<BYTE>& 类型的参数即可。
      

  5.   

    搂住的函数的意思可能是 在 while(){} 中 可以 对 BYTE *x 对象可以动态分配 ,solike 的 函数传入的参数 BYTE * x ,long len  , 必须在外面  分配了 。所以最好是可以 用类包装 BYTE  
    class MyByte { 
      private:  
           BYTE * M_pByte;
           LONG  m_lLength ;
    MyByte* pThis ;
       public:
           Length(){ return m_lLength ;} 
           BYTE operator[]( int i) { return m_pByte[i] } 
           MyByte& operator new (int size) {           m_pByte  = new BYTE[size/sizeof(MyByte)];
      
               return m_pThis =allocate(sizeof(MyByte)); 
           }
          MyByte& operator  delete[](){...}       MyByte(){...}
    }
    MyByte* p  = new MyByte[100];// equal  BYTE m_pByte = new Byte[100];