将C#转换为VB代码

时间:2015-11-25 13:43:47

标签: vb.net c3.js

我有这个用于创建CRC代码的c#代码。它在c#中正常工作。我将其转换为VB,但它没有生成正确的CRC代码。有人可以帮我弄清楚VB代码有什么问题。

使用字符串调用GetMessageBytes并返回添加了CRC的字节数组

    static byte[] GetMessageBytes(string text)
        {
        //Get bytes for command
        byte[] command = Encoding.ASCII.GetBytes(text);

        //Get CRC for command bytes
        ushort crc = CalculateCrc(command);

        //Append CRC and CR to command
        byte[] result = new byte[command.Length + 3];
        command.CopyTo(result, 0);
        result[result.Length - 3] = (byte)((crc >> 8) & 0xFF);
        result[result.Length - 2] = (byte)((crc >> 0) & 0xFF);
        result[result.Length - 1] = 0x0d;

        return result;
    }


    static ushort CalculateCrc(byte[] pin)
    {
        ushort crc;
        byte da;
        byte ptr;
        byte bCRCHign;
        byte bCRCLow;

        int len = pin.Length;

        ushort[] crc_ta = new ushort[]
            { 
                0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
                0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef
            };

        crc = 0;
        for (int index = 0; index < len; index++)
        {
            ptr = pin[index];

            da = (byte)(((byte)(crc >> 8)) >> 4);
            crc <<= 4;
            crc ^= crc_ta[da ^ (ptr >> 4)];
            da = (byte)(((byte)(crc >> 8)) >> 4);
            crc <<= 4;
            crc ^= crc_ta[da ^ (ptr & 0x0f)];
        }

        //Escape CR,LF,'H' characters
        bCRCLow = (byte)(crc & 0x00FF);
        bCRCHign = (byte)(crc >> 8);
        if (bCRCLow == 0x28 || bCRCLow == 0x0d || bCRCLow == 0x0a)
        {
            bCRCLow++;
        }
        if (bCRCHign == 0x28 || bCRCHign == 0x0d || bCRCHign == 0x0a)
        {
            bCRCHign++;
        }
        crc = (ushort)(((ushort)bCRCHign) << 8);
        crc |= bCRCLow;
        return crc;
    }

这是VB代码。

    Private Function GetMessageBytes(text As String) As Byte()
    'Get bytes for command
    Dim command As Byte() = System.Text.Encoding.Unicode.GetBytes(text)

    'Get CRC for command bytes
    Dim crc As UShort = CalcCrcHalf(command)

    'Append CRC and CR to command
    Dim result As Byte() = New Byte(command.Length + 3) {}
    command.CopyTo(result, 0)
    result(result.Length - 3) = CByte((crc >> 8) And &HFF)
    result(result.Length - 2) = CByte((crc >> 0) And &HFF)
    result(result.Length - 1) = &HD

    Return result
End Function


Private Function CalculateCRC(pin As Byte()) As UShort
    Dim crc As UShort
    Dim da As Byte
    Dim ptr As Byte
    Dim bCRCHign As Byte
    Dim bCRCLow As Byte

    Dim len As Integer = pin.Length

    Dim crc_ta As UShort() = New UShort() {&H0, &H1021, &H2042, &H3063, &H4084, &H50A5, &H60C6, &H70E7, &H8108, &H9129, &HA14A, &HB16B, &HC18C, &HD1AD, &HE1CE, &HF1EF}

    crc = 0
    For index As Integer = 0 To len - 1
        ptr = pin(index)
        da = CByte(CByte(crc >> 8) >> 4)
        crc <<= 4
        crc = crc Xor crc_ta(da Xor (ptr >> 4))
        da = CByte(CByte(crc >> 8) >> 4)
        crc <<= 4
        crc = crc Xor crc_ta(da Xor (ptr And &HF))
    Next

    'Escape CR,LF,'H' characters
    bCRCLow = CByte(crc And &HFF)
    bCRCHign = CByte(crc >> 8)
    If bCRCLow = &H28 OrElse bCRCLow = &HD OrElse bCRCLow = &HA Then
        bCRCLow += 1
    End If
    If bCRCHign = &H28 OrElse bCRCHign = &HD OrElse bCRCHign = &HA Then
        bCRCHign += 1
    End If
    crc = CUShort(CUShort(bCRCHign) << 8)
    crc = crc Or bCRCLow

    Return crc

End Function

1 个答案:

答案 0 :(得分:0)

谢谢我让它运转起来。这是编码。

这是正确的代码。

    retrun_string = System.Text.Encoding.GetEncoding(1252).GetString(result)