Vb.net Visual Basic-添加两个二进制数

Vb.net Visual Basic-添加两个二进制数,vb.net,binary,addition,Vb.net,Binary,Addition,嗨,我是这里的新手,一般都是编码方面的新手。我一直在编写将两个二进制数相加的代码,单独的部分可以工作,但合并的部分不能。如果有人能帮我解决这个问题,我将非常感激。如果可以缩短,也会有帮助,谢谢 Class Form1 Dim intNum1 As Integer Dim intNum2 As Integer Dim intNum3 As Integer Public Function BinaryToDecimalA(ByRef Binary As Strin

嗨,我是这里的新手,一般都是编码方面的新手。我一直在编写将两个二进制数相加的代码,单独的部分可以工作,但合并的部分不能。如果有人能帮我解决这个问题,我将非常感激。如果可以缩短,也会有帮助,谢谢

Class Form1

    Dim intNum1 As Integer
    Dim intNum2 As Integer
    Dim intNum3 As Integer

    Public Function BinaryToDecimalA(ByRef Binary As String) As Integer
        Dim BinaryNumA As Integer
        Dim BitCountA As Short

        For BitCountA = 1 To Len(Binary)
            BinaryNumA = BinaryNumA + (CDbl(Mid(Binary, Len(Binary) - BitCountA + 1, 1)) * (2 ^ (BitCountA - 1)))
        Next BitCountA
        BinaryToDecimalA = BinaryNumA

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        intNum1 = (BinaryToDecimal((TextBox1.Text)))

    End Sub
    Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
            e.Handled = True
        End If

        TextBox1.MaxLength = 3
    End Sub
    Public Function BinaryToDecimal(ByRef Binary As String) As Integer
        Dim BinaryNum As Integer
        Dim BitCount As Short

        For BitCount = 1 To Len(Binary)
            BinaryNum = BinaryNum + (CDbl(Mid(Binary, Len(Binary) - BitCount + 1, 1)) * (2 ^ (BitCount - 1)))
        Next BitCount
        BinaryToDecimal = BinaryNum

    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        intNum2 = (BinaryToDecimal((TextBox2.Text)))

    End Sub
    Private Sub TextBox2_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress

        If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
            e.Handled = True
        End If

        TextBox2.MaxLength = 3
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        intNum1 = Integer.Parse(TextBox1.Text)

        intNum2 = Integer.Parse(TextBox2.Text)

        intNum3 = intNum1 + intNum2

    End Sub

    Private Sub intNum3_TextChanged(sender As Object, e As EventArgs) Handles TextBoxAns.TextChanged

        Dim i As Long, x As Long, bin As String
        Const maxpower = 7
        TextBoxAns.Enabled = False
        bin = ""
        x = Val(intNum3)

        If x > 2 ^ maxpower Then
            MsgBox("Number must be no longer than " & Str$(2 ^ maxpower))
            TextBoxAns.Text = ""
        End If

        If x < 0 Then bin = bin + "1" Else bin = bin + "0"

        For i = maxpower To 0 Step -1
            If x And (2 ^ i) Then
                bin = bin + "1"
            Else
                bin = bin + "0"
            End If
        Next
        TextBoxAns.Text = bin
    End Sub
End Class
类格式1
Dim intNum1为整数
Dim intNum2为整数
Dim intNum3为整数
作为整数的公共函数BinaryToDecimalA(ByRef Binary作为字符串)
作为整数的Dim二进制numa
像短的一样暗淡
对于BitCountA=1到Len(二进制)
BinaryNumA=BinaryNumA+(CDbl(Mid(Binary,Len(Binary)-BitCountA+1,1))*(2^(BitCountA-1)))
下一个比特伯爵
BinaryToDecimalA=BinaryNumA
端函数
私有子按钮1\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮1。单击
intNum1=(二进制到十进制((TextBox1.Text)))
端接头
私有子文本框1u KeyPress(ByVal sender作为System.Object,ByVal e作为System.Windows.Forms.KeyPressEventArgs)处理文本框1.KeyPress
如果不是Char.IsDigit(e.KeyChar)而不是Char.IsControl(e.KeyChar),则
e、 已处理=真
如果结束
TextBox1.MaxLength=3
端接头
作为整数的公共函数BinaryToDecimal(ByRef Binary作为字符串)
作为整数的Dim BinaryNum
将位计数设置为短
对于比特数=1到Len(二进制)
BinaryNum=BinaryNum+(CDbl(Mid(Binary,Len(Binary)-BitCount+1,1))*(2^(BitCount-1)))
下一位计数
BinaryToDecimal=BinaryNum
端函数
私有子按钮2\u单击(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理按钮1。单击
intNum2=(BinaryToDecimal((TextBox2.Text)))
端接头
私有子textbox2keypress(ByVal发送者作为System.Object,ByVal e作为System.Windows.Forms.KeyPressEventArgs)处理TextBox2.KeyPress
如果不是Char.IsDigit(e.KeyChar)而不是Char.IsControl(e.KeyChar),则
e、 已处理=真
如果结束
TextBox2.MaxLength=3
端接头
私有子按钮3\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮1。单击
intNum1=Integer.Parse(TextBox1.Text)
intNum2=Integer.Parse(TextBox2.Text)
intNum3=intNum1+intNum2
端接头
私有子intNum3_TextChanged(发送方作为对象,e作为事件参数)处理TextBoxAns.TextChanged
尺寸i为长,x为长,bin为字符串
常数maxpower=7
TextBoxAns.Enabled=False
bin=“”
x=Val(整数m3)
如果x>2^maxpower,则
MsgBox(“编号不得超过”&Str$(2^maxpower))
TextBoxAns.Text=“”
如果结束
如果x<0,则bin=bin+“1”,否则bin=bin+“0”
对于i=maxpower至0,步骤-1
如果x和(2^i)那么
bin=bin+“1”
其他的
bin=bin+“0”
如果结束
下一个
TextBoxAns.Text=bin
端接头
末级
这应该可以- 不是我的代码(参考),但它的工作

公共函数二进制加法(ByVal A作为字符串,ByVal B作为字符串)作为字符串

货币

curA = BinToDec(A)
货币

curA = BinToDec(A)
作为货币的暗淡货币

curA = BinToDec(A)
路缘=BinToDec(B)

端函数

Module Module1
    Sub Main()
        Console.WriteLine("Enter the first binary number, then enter the second binary number")
        Dim num As Integer = Convert.ToInt32(Console.ReadLine, 2)
        Console.WriteLine(Convert.ToString(num + Convert.ToInt32(Console.ReadLine, 2), 2))
        Console.ReadLine()
    End Sub
End Module
私有函数DecToBin(curDec作为货币)作为字符串

将strTemp设置为字符串

作为整数的Dim i

i = 31

Do While i >= 0
    If curDec >= (2 ^ i) Then
        strTemp = strTemp & "1"
        curDec = curDec - (2 ^ i)
    Else
        strTemp = strTemp & "0"
    End If

    i = i - 1
Loop

DecToBin = strTemp
结束功能

这应该可以-
Module Module1
    Sub Main()
        Console.WriteLine("Enter the first binary number, then enter the second binary number")
        Dim num As Integer = Convert.ToInt32(Console.ReadLine, 2)
        Console.WriteLine(Convert.ToString(num + Convert.ToInt32(Console.ReadLine, 2), 2))
        Console.ReadLine()
    End Sub
End Module
不是我的代码(参考),但它的工作

公共函数二进制加法(ByVal A作为字符串,ByVal B作为字符串)作为字符串

货币

curA = BinToDec(A)
货币

curA = BinToDec(A)
作为货币的暗淡货币

curA = BinToDec(A)
路缘=BinToDec(B)

端函数

Module Module1
    Sub Main()
        Console.WriteLine("Enter the first binary number, then enter the second binary number")
        Dim num As Integer = Convert.ToInt32(Console.ReadLine, 2)
        Console.WriteLine(Convert.ToString(num + Convert.ToInt32(Console.ReadLine, 2), 2))
        Console.ReadLine()
    End Sub
End Module
私有函数DecToBin(curDec作为货币)作为字符串

将strTemp设置为字符串

作为整数的Dim i

i = 31

Do While i >= 0
    If curDec >= (2 ^ i) Then
        strTemp = strTemp & "1"
        curDec = curDec - (2 ^ i)
    Else
        strTemp = strTemp & "0"
    End If

    i = i - 1
Loop

DecToBin = strTemp

结束函数

dim a As Integer=Convert.ToInt32(“101010”,2)+Convert.ToInt32(“1000101”,2)
dim a As Integer=Convert.ToInt32(“101010”,2)+Convert.ToInt32(“1000101”,2)
谢谢你的代码,但我真的需要它以我上面创建的代码为基础。谢谢你的代码,但我真的需要它以我上面创建的代码为基础。
Module Module1
    Sub Main()
        Console.WriteLine("Enter the first binary number, then enter the second binary number")
        Dim num As Integer = Convert.ToInt32(Console.ReadLine, 2)
        Console.WriteLine(Convert.ToString(num + Convert.ToInt32(Console.ReadLine, 2), 2))
        Console.ReadLine()
    End Sub
End Module