Visual Basic - 添加两个二进制数

时间:2014-04-04 13:49:56

标签: vb.net binary addition

嗨,我在这里和一般的编码新。我一直在研究这个代码,将两个二进制数加在一起,各个部分都可以工作,但是不能合并。如果有人能帮我解决这个问题,我将非常感激。如果它可以缩短也有帮助,谢谢。

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

2 个答案:

答案 0 :(得分:0)

这应该有效 - 不是我的代码(参考http://www.bigresource.com/VB-Converting-a-number-to-its-binary-number-uDbSei3kPM.html),但它有效!

Public Function BinaryAddition(ByVal A As String,ByVal B As String)As String

Dim curA As Currency

Dim curB As Currency

Dim curResult As Currency

curA = BinToDec(A)

curB = BinToDec(B)

curResult = (curA + curB) ' Mod (2 ^ 32)

BinaryAddition = DecToBin(curResult)

结束功能

私有函数DecToBin(curDec As Currency)字符串

Dim strTemp As String

Dim i As Integer

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

结束功能

答案 1 :(得分:-1)

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