当前位置:编程学习 > VB >>

VB代码 转换C#代码

第一段:

Dim a(8), b(8), c(8) As Single
For x = 1 To 8
a(x) = 0
b(x) = 0
c(x) = 0
Next x
N = Len(dec2bin("&h" & Text4(1).Text))
For x = 1 To N
   a(x) = Mid((dec2bin("&h" & Text4(1).Text)), N - x + 1, 1)
Next x
N = Len(dec2bin("&h" & Text4(2).Text))
For x = 1 To N
   b(x) = Mid((dec2bin("&h" & Text4(2).Text)), N - x + 1, 1)
Next x
N = Len(dec2bin("&h" & Text4(3).Text))
For x = 1 To N
   c(x) = Mid((dec2bin("&h" & Text4(3).Text)), N - x + 1, 1)
Next x

 

第二段:

 

   On Error Resume Next
    Dim buffer() As Byte
    Dim i As Integer
    Select Case MSComm3.CommEvent
        Case comEvReceive
        buffer = MSComm3.Input
        For i = 0 To UBound(buffer)
            If Len(Hex(buffer(i))) = 1 Then
                strData = strData & "0" + Hex(buffer(i))
            Else
                strData = strData & Hex(buffer(i))
            End If
        Next i
            Text4(0).Text = Mid(strData, 1, 2)
            Text4(1).Text = Mid(strData, 3, 2)
            Text4(2).Text = Mid(strData, 5, 2)
            Text4(3).Text = Mid(strData, 7, 2)
            Text4(4).Text = Mid(strData, 9, 2)
            strData = ""
               If Text4(4).Text = Right("0" & Hex(Val("&h" & Text4(0).Text) + Val("&h" & Text4(1).Text) + Val("&h" & Text4(2).Text) + Val("&h" & Text4(3).Text)), 2) Then
                  Call yunxian
'                        Text2(0).Text = Text4(0).Text
'                        Text2(1).Text = Text4(1).Text
'                        Text2(2).Text = Text4(2).Text
'                        Call yunxian1
               Else
                    Text4(0).Text = ""
                    Text4(1).Text = ""
                    Text4(2).Text = ""
                    Text4(3).Text = ""
                    Text4(4).Text = ""
               End If
               MSComm3.InBufferCount = 0
    End Select

答案:第一段
Single[] a = new Single[8];
            Single[] b = new Single[8];
            Single[] c = new Single[8];
            int x;
            for (x = 1; x <= 8; x++)
            {
                a[x] = 0;
                b[x] = 0;
                c[x] = 0;
            }
            N = (dec2bin("0x" + Text4[1].Text)).ToString().Length;
            for (x = 1; x <= N; x++)
            {
                a[x] = (dec2bin("0x" + Text4[1].Text)).ToString().Substring(N - x + 1, 1);
            }
            N = (dec2bin("0x" + Text4(2).Text)).ToString().Length;
            for (x = 1; x <= N; x++)
            {
                b[x] = (dec2bin("0x" + Text4[2].Text)).ToString().Substring(N - x + 1, 1);
            }
            N = (dec2bin("0x" + Text4(3).Text)).ToString().Length;
            for (x = 1; x <= N; x++)
            {
                c[x] = (dec2bin("0x" + Text4[3].Text)).ToString().Substring(N - x + 1, 1);
            } 
 
第二段
byte[] buffer;
            int i;
            switch (MSComm3.CommEvent)
            {
                case comEvReceive:
                    buffer = MSComm3.Input;
                    for (i = 0; i <= UBound(buffer); i++)
                    {
                        if (Hex(buffer[i]).ToString().Length == 1)
                        {
                            strData = strData + "0" + Hex(buffer[i]);
                        }
                        else
                        {
                            strData = strData + Hex(buffer[i]);
                        }
                        Text4[0].Text = strData.Substring(1, 2);
                        Text4[1].Text = strData.Substring(3, 2);
                        Text4[2].Text = strData.Substring(5, 2);
                        Text4[3].Text = strData.Substring(7, 2);
                        Text4[4].Text = strData.Substring(9, 2);
                        strData = "";
                        string txt = "0" + Hex(Val("0x" + Text4[0].Text) + Val("0x" + Text4[1].Text) + Val("0x" + Text4[2].Text) + Val("0x" + Text4[3].Text));
                        if (Text4[4].Text == txt.Substring(txt.Length - 2, 2))
                        {
                            Text4[0].Text = "";
                            Text4[1].Text = "";
                            Text4[2].Text = "";
                            Text4[3].Text = "";
                            Text4[4].Text = "";
                        }
                        MSComm3.InBufferCount = 0;
                    }
                    break;
            }
 
花了不少时间转写为C#了,因为给的代码不是很全,所以,转换了95%这样。。不过还是希望对你有帮助。。^^

上一个:VB 修改注册表dword值
下一个:vb鼠标滚轮问题

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,