需要更多帮助将CRC函数从VB.NET转换为C#

And*_*ers 2 c# code-conversion

我可能应该把所有这些都放在一个问题中,抱歉:(

第二个错误

好的,这应该是错误的结束(希望如此):

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();
}
Run Code Online (Sandbox Code Playgroud)

错误在返回行:

编译器错误消息: CS1502:'CRC.CRC32.GetCrc32(ref 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);

}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我试图将内存流作为一个流进行转换,但我认为它不太喜欢它.

第一个错误

(这里解决了)

如果你还没有看到我的最后几个问题,我有一个用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
Run Code Online (Sandbox Code Playgroud)

这是我转换的代码.我解决了我遇到的一些错误,但还有一些我无法弄清楚:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

第一个错误是在这一行:

编译器错误消息: CS0030:无法将类型'uint'转换为'bool'

if ((dwCrc & 1))

我应该将这两个值与不同的运算符进行比较吗?我不太确定为什么&运营商要诚实.

谢谢你.

Pav*_*aev 7

C#中没有隐式的整数到布尔转换.你必须比较0明显:

if ((dwCrc & 1) != 0) ...
Run Code Online (Sandbox Code Playgroud)