Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 将C转换为VB代码_Vb.net_C3.js - Fatal编程技术网

Vb.net 将C转换为VB代码

Vb.net 将C转换为VB代码,vb.net,c3.js,Vb.net,C3.js,我有一个创建CRC代码的c代码。它在c语言中工作正常。我将其转换为VB,但它没有生成正确的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 com

我有一个创建CRC代码的c代码。它在c语言中工作正常。我将其转换为VB,但它没有生成正确的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
使用字符串调用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

谢谢,我让它工作了。这是编码

这是正确的代码

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

最明显的是,您的编码在GetMessageBytes中是不同的。在C中,您使用ASCII,而在VB中使用Unicode。我将打开两个visual studio,一个使用VB,另一个使用C,并逐行遍历这两个visual studio,查看变量从何处获得不同的值。不需要两个实例,VS能够在一个解决方案中启动/调试多个应用程序,以便。。。。大约10年了。欢迎来到2015年。