C#函数对Visual Basic 2010错误的BitShift运算符“<<&”;

本文关键字:lt 运算符 BitShift 函数 Visual Basic 错误 2010 | 更新日期: 2023-09-27 18:23:35

我目前正在将C#代码翻译成Visual Basic 2010。效果很好,但现在我没有得到正确的线路。每次我得到一个位偏移错误

原始代码看起来像:

代码:

private static string Checksum(string url)
    {
        uint Magic = 0xE6359A60;
        uint a, b;
        uint c = Magic;
        a = b = 0x9E3779B9;
        int k = 0;
        int length = url.Length;
        //Algorithm
        while (length >= 12)
        {
            a += (uint)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24));
            b += (uint)(url[k + 4] + (url[k + 5] << 8) + (url[k + 6] << 16) + (url[k + 7] << 24));
            c += (uint)(url[k + 8] + (url[k + 9] << 8) + (url[k + 10] << 16) + (url[k + 11] << 24));
            Mix(ref a, ref b, ref c);
            k += 12;
            length -= 12;
        }

我的翻译是:

Private Function Checksum(ByVal url) As String
    Dim Magic As UInteger = &HE6359A60UI
    Dim a As UInteger, b As UInteger
    Dim c As UInteger =Magic
    a = b = &H9E3779B9UI
    Dim k As Integer = 0
    Dim length As Integer = url.Length

    'Algorithm
    While length >= 12
        a += CUInt(url(k + 0) + (url(k + 1) << 8) + (url(k + 2) << 16) + (url(k + 3) << 24))
        b += CUInt(url(k + 4) + (url(k + 5) << 8) + (url(k + 6) << 16) + (url(k + 7) << 24))
        c += CUInt(url(k + 8) + (url(k + 9) << 8) + (url(k + 10) << 16) + (url(k + 11) << 24))
        Mix(a, b, c)
        k += 12
        length -= 12
    End While

但是当调试器进入时

a += CUInt(url(k + 0) + (url(k + 1) << 8) + (url(k + 2) << 16) + (url(k + 3) << 24))

它抛出一个异常:没有为Typ-Char定义运算符"<<"。

C#函数对Visual Basic 2010错误的BitShift运算符“<<&”;

在C#中,编译器会自动将char转换为int。由于VB编译器不能做到这一点,您必须进行显式转换:

a += CUInt(AscW((url(k + 0)) + (AscW(url(k + 1)) << 8) + (AscW(url(k + 2)) << 16) + (AscW(url(k + 3)) << 24))
a += CUInt(AscW(url(k + 0))) + (CUInt(AscW(url(k + 1))) << 8) + (CUInt(AscW(url(k + 2))) << 16) + (CUInt(AscW(url(k + 3))) << 24)

假设您的字符是Unicode。如果是ASCII,请使用Asc()而不是AscW()