2009-07-24 32 views
2

我也許應該只是把這個都在同一個問題,對不起爲:(需要多一點幫助,從VB.NET轉換CRC功能,C#

二錯誤

好吧,這應該是錯誤的端部(希望):

private static string getCRC(string input, bool appendDate) 
{ 
    string toEnc = string.Format("{0}{1}", input, appendDate == true ? string.Format("{0:yyyyMMdd}", System.DateTime.Now) : ""); 
    System.IO.MemoryStream mStream = new System.IO.MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(toEnc), false); 

    CRC32 oCRC = new CRC32(); 
    return oCRC.GetCrc32(((System.IO.Stream)mStream)).ToString(); 
} 

錯誤是在迴流管線:

編譯器錯誤消息: CS1502:爲了獲得最佳重載方法匹配「CRC.CRC32.GetCrc32(參照System.IO.Stream)」具有一些無效參數

下面是函數它指的是:

public UInt32 GetCrc32(ref System.IO.Stream stream) 
{ 
    //Dim crc32Result As Integer = &HFFFFFFFF 
    UInt32 crc32Result = UInt32.MaxValue; 

    try 
    { 

     byte[] buffer = new byte[BUFFER_SIZE]; 
     int readSize = BUFFER_SIZE; 

     int count = stream.Read(buffer, 0, readSize); 
     int i; 
     UInt32 iLookup; 
     //Dim tot As Integer = 0 
     while ((count > 0)) 
     { 
      for (i = 0; i <= count - 1; i++) 
      { 
       iLookup = (crc32Result & 0xff)^buffer[i]; 
       //crc32Result = ((crc32Result And &HFFFFFF00) \ &H100) And &HFFFFFF      ' nasty shr 8 with vb :/ 
       crc32Result = ((UInt32)((crc32Result & 4294967040L)/256) & UInt32.MaxValue); 
       // nasty shr 8 with vb :/ 
       crc32Result = crc32Result^crc32Table[iLookup]; 
      } 
      count = stream.Read(buffer, 0, readSize); 
     } 
    } 
    catch (Exception ex) 
    { 
     HttpContext.Current.Response.Output.Write(string.Format("{0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace)); 
     System.Diagnostics.Debugger.Break(); 
    } 

    return (~crc32Result); 

} 

正如你所看到的,我試圖將流媒體視爲一個流,但我不認爲它太喜歡。

第一個錯誤

solved here

如果您還沒有看到我的最後幾個問題,我已經寫在VB.NET一個CRC功能。我使用在線轉換器將其轉換爲C#。這裏是原始的VB代碼:

Public Sub New() 
    Dim dwPolynomial As UInt32 = 3988292384 
    Dim i As Integer, j As Integer 

    ReDim crc32Table(256) 
    Dim dwCrc As UInt32 

    For i = 0 To 255 
     dwCrc = i 
     For j = 8 To 1 Step -1 
      If (dwCrc And 1) Then 
       dwCrc = ((dwCrc And &HFFFFFFFE) \ 2&) And &H7FFFFFFF 
       dwCrc = dwCrc Xor dwPolynomial 
      Else 
       dwCrc = ((dwCrc And &HFFFFFFFE) \ 2&) And &H7FFFFFFF 
      End If 
     Next j 
     crc32Table(i) = dwCrc 
    Next i 
End Sub 

這是我的轉換代碼。我定了幾個我有錯誤的,但也有一些更多的,我想不通:

public CRC32() 
{ 
    UInt32 dwPolynomial = ((UInt32)3988292384L); 
    int i; 
    int j; 

    UInt32[] crc32Table; 
    crc32Table = new UInt32[256]; 
    UInt32 dwCrc; 

    for (i = 0; i <= 255; i++) 
    { 
     dwCrc = ((UInt32)i); 
     for (j = 8; j >= 1; j -= 1) 
     { 
      if ((dwCrc & 1)) 
      { 
       dwCrc = ((dwCrc & 0xfffffffe)/2L) & 0x7fffffff; 
       dwCrc = dwCrc^dwPolynomial; 
      } 
      else 
      { 
       dwCrc = ((dwCrc & 0xfffffffe)/2L) & 0x7fffffff; 
      } 
     } 
     crc32Table[i] = dwCrc; 
    } 
} 

第一個錯誤是在這條線:

編譯器錯誤信息: CS0030:無法將類型 'UINT' 到 '布爾'

if ((dwCrc & 1))

我應該比較這兩個值與不同的運算符?我不太確定爲什麼&運營商是誠實的。

謝謝。

+0

你的VB.NET代碼有錯誤。 ReDim crc32Table(256)給你25 * 7 *的條目,但你只填寫256.轉換搞砸了(正確的C#相當於新的uint [257]),但實際上它適合你的情況。 ;) – 2009-07-24 20:17:01

回答

7

在C#中沒有隱式的整數到布爾轉換。你必須比較0顯式:

if ((dwCrc & 1) != 0) ... 
2

C#不要假設0等於false,並且1等於true。

因此

if ((dwCrc & 1) == 1) { } 

會是你所需要的。

+0

VB.NET轉換!= 0是真的....微妙不同 – 2009-07-24 20:14:43

0

該特定行是按位與邏輯和dwCrc和1,它只是0x01。所以基本上,它檢查dwCrc的最低有效位是1,而不是0.如果是,結果將爲1.如果不是,則結果爲0。在VB中,整數自動轉換爲布爾(0 - > false,其他 - > true)。在C#中,你必須將它與一個值進行比較明確

if ((dwCrc & 1) != 0) ... 

(另請注意,是大概需要(& 1),因爲它是檢查只有一個特定的位。如果dwCrc == 0×02,則原始的測試會失敗,這將轉換後的一個。所以做這樣

if(dwCrc != 0) ... 

的東西是錯誤的,雖然它似乎沒有可能那麼在第一。)

1

隨着ReSharper的幫助,我得到了你的變換代碼編譯。

嘗試了這一點:

public void CRC32() 
    { 
     UInt32 dwPolynomial = ((UInt32) 3988292384L); 
     int i; 
     int j; 

     UInt32[] crc32Table; 
     crc32Table = new UInt32[256]; 
     UInt32 dwCrc; 

     for (i = 0; i <= 255; i++) 
     { 
      dwCrc = ((UInt32) i); 
      for (j = 8; j >= 1; j -= 1) 
      { 
       if ((dwCrc & 1) != 0u) 
       { 
        dwCrc = (uint) (((dwCrc & 0xfffffffe)/2L) & 0x7fffffff); 
        dwCrc = dwCrc^dwPolynomial; 
       } 
       else 
       { 
        dwCrc = (uint) (((dwCrc & 0xfffffffe)/2L) & 0x7fffffff); 
       } 
      } 

      crc32Table[i] = dwCrc; 
     } 
    } 
0

貌似很多這些答覆解決您的第一個錯誤。第二個錯誤的問題是您的GetCrc方法期望流通過引用傳遞(ref關鍵字)。

在C#中,如果方法通過引用定義參數,則必須在調用它時通過引用顯式傳遞它。所以加ref關鍵字,當你打電話給你GetCrcMethod:

(也,你不需要投 - MemoryStream的自Stream繼承)

return oCrc.GetCrc32(ref mStream).ToString();

另一個考慮: 除非你打算改變其strem mStream引用,您不需要創建byRef參數。除非您打算爲流分配一個值(stream = new stream或stream = null),否則只需從方法聲明中刪除ref關鍵字就更爲正確。

2

對於您的第二個問題(這會得到更多的流量,如果你張貼的第二個問題):

爲「CRC.CRC32.GetCrc32的最佳重載的方法匹配(REF System.IO.Stream )」有一些無效參數

在C#代碼刪除ref修改:

public UInt32 GetCrc32(ref System.IO.Stream stream) 

變得

public UInt32 GetCrc32(System.IO.Stream stream) 

我懷疑原來的VB.NET有沒有必要的ByRef - 我沒有看到你在任何地方重新分配stream的值。

或者,ref參數調用它:

return oCRC.GetCrc32(ref mStream).ToString();