2012-08-23 56 views
0

我想將我在Delphi中的desencrypt函數移植到C#中。我在delphi中使用函數Move來獲取數據到內存中,所以我嘗試使用marshal.copy函數的結果相同,但有時候我會得到一個acces違例異常,有時候也會出現空格。 請留下該函數的代碼。 任何想法?非常感謝!如何使用marshal.Copy函數將數據讀入內存?

public string prDecode(string s) 
    { 
     byte[] ArrayMap = new byte[256] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
             0, 0, 0, 62, 0, 0, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59,60, 
             61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
             11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 
             0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 
             36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 
             51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
     int I; 
     char[] ArrayS = s.ToCharArray(0, s.Length); 
     string resultado = ""; 
     char[] ArrayR = resultado.ToCharArray(0, resultado.Length); 
     switch (s.Length) 
     { 
      case 2: 
       I = ArrayMap[ArrayS[0]] + (ArrayMap[ArrayS[1]] << 6); 
       ArrayR = new char[1]; 
       Marshal.Copy((IntPtr)I, ArrayR, 0, ArrayR.Length); 

       break; 
      case 3: 
       I = ArrayMap[ArrayS[0]] + (ArrayMap[ArrayS[1]] << 6) + 
        (ArrayMap[ArrayS[2]] << 12); 
       ArrayR = new char[2]; 
       Marshal.Copy((IntPtr)I, ArrayR, 0, ArrayR.Length); 
       break; 

      case 4: 
       I = ArrayMap[ArrayS[0]] + (ArrayMap[ArrayS[1]] << 6) + 
        (ArrayMap[ArrayS[2]] << 12) + (ArrayMap[ArrayS[3]] << 18); 
       ArrayR = new char[3]; 
       Marshal.Copy((IntPtr)I, ArrayR, 0, ArrayR.Length); 
       break; 
     } 
     string clave = new string (ArrayR); 
     return clave; 

    } 

回答

1

沒有必要使用統帥,除非您訪問非託管內存的遺留代碼。託管代碼不需要指針&使用託管方法會快得多。

你在找什麼我想,是Array.Copy使用託管代碼在數組之間進行復制。

相關問題