Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27514

Can Anyone Improve My Binary to Denary Code

$
0
0
Hi there,

I was doing a Binary to Denary calculator as part of a school project and I have accomplished the aim but I feel it could be easier to use. Does anyone know how I can manipulate my code so instead of writing each binary digit individually the user can just type the whole binary string in. Also any error trapping will be of assistance.

Here is my code:
Code:

Module Module1
    Sub Main()
        Dim Binary(7) As String
        Dim BinaryBuild As String = ""
        Dim IntegerBuild As Integer = 0
        Dim Denary As Integer = 0

        Binary(0) = "" 'resets the array
        Binary(1) = "" 'resets the array
        Binary(2) = "" 'resets the array
        Binary(3) = "" 'resets the array
        Binary(4) = "" 'resets the array
        Binary(5) = "" 'resets the array
        Binary(6) = "" 'resets the array

Start:  Console.WriteLine("Enter each digit of the Binary number seperately to convert")

        For i = 0 To 7
            Console.WriteLine(BinaryBuild)
            Binary(i) = Console.ReadLine()
            If Binary(i) = 0 Or Binary(i) = 1 Then
                BinaryBuild = BinaryBuild & Binary(i) '
            Else
                Console.WriteLine("Error") 'tells user that the digit is invalid and asks them to enter it again
                Console.Clear()
                GoTo Start
            End If
        Next

        For i = 7 To 0 Step -1
            Denary = Denary + (Binary(i)) * (2 ^ BinaryBuild)
            BinaryBuild = BinaryBuild + 1
        Next

        Console.WriteLine(Denary)

        Console.ReadLine()


    End Sub
End Module


Viewing all articles
Browse latest Browse all 27514

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>