public static UInt32[] ToUInt32Array(string input, Boolean IncludeLength)
        {            Byte[] Data=System.Text.Encoding.UTF8.GetBytes(input);
            Int32 n = (((Data.Length & 3) == 0) ? (Data.Length >> 2) : ((Data.Length >> 2) + 1));
            UInt32[] Result;
            if (IncludeLength)
            {
                Result = new UInt32[n + 1];
                Result[n] = (UInt32)Data.Length;
            }
            else
            {
                Result = new UInt32[n];
            }
            n = Data.Length;
            for (Int32 i = 0; i < n; i++)
            {
                Result[i >> 2] |= (UInt32)Data[i] << ((i & 3) << 3);
            }
            return Result;
        }

解决方案 »

  1.   

    用Delphi写了一个类似函数,不知正确与否。返回值用完后必须用FreeMem释放,因为Delphi没有自动回收内存机制。function ToUInt32Array(input: string; IncludeLength: Boolean): PLongWord;
    type
      UWArray = array of LongWord;
    var
      n, i: Integer;
    begin
      n := (Length(input) + 3) shr 2;
      if IncludeLength then
      begin
        GetMem(Result, (n + 1) * Sizeof(LongWord));
        UWArray(Result)[n] := Length(input);
      end else
        GetMem(Result, n * Sizeof(LongWord));
      n := Length(input) - 1;
      for i := 0 to n do
        UWArray(Result)[I shr 2] := UWArray(Result)[I shr 2] or ((LongWord(Ord(input[i + 1])) shl ((i and 3) shl 3)));
    end;
      

  2.   

    上面的返回值是指针,使用起来可能麻烦,而且用毕还得释放,修改了一下,直接用动态数组,比较符合原函数意。type
      UWArray = array of LongWord;function ToUInt32Array(input: string; IncludeLength: Boolean): UWArray;
    var
      n, i: Integer;
    begin
      n := (Length(input) + 3) shr 2;
      if IncludeLength then
      begin
        SetLength(Result, n + 1);
        Result[n] := Length(input);
      end else
        SetLength(Result, n);
      n := Length(input) - 1;
      for i := 0 to n do
        Result[I shr 2] :=Result[I shr 2] or ((LongWord(Ord(input[i + 1])) shl ((i and 3) shl 3)));
    end;