Resolved binary to denary and reverse converter

qaisqais

Member
Joined
Mar 29, 2010
Messages
14
Programming Experience
Beginner
hi, for homework an optional task was to create a binary to denary and denary to binary converter, using console application in VB.NET .. anyway i gave it my best shot however and it all works except for the denary to binary conversion, can someone please help me out? not been programming for very long so my code might be a bit messy or badly written i dont know, but if someone can help me fix this itd be really appreciated..

VB.NET:
Module Module1

    Sub Main()
        'Declare variables
        Dim subjump As String
        Dim retry As Char


        'Set console window dimensions, add title to console window
        Console.WindowHeight = 30
        Console.WindowWidth = 100
        Console.Title = "Binary<->Denary Converter"

        'Repeat label 
repeat:

        'Prompt user to select type of conversion, read into the subroutine jump variable after being uppercased
        Console.WriteLine("Please select conversion type(For Binary->Denary type BIN, for Denary->Binary type DEN)")
        subjump = UCase(Console.ReadLine)

        'Redirects to appropriate subroutine depending on requested conversion type
        If subjump = "BIN" Then
            BIN()
        ElseIf subjump = "DEN" Then
            DEN()
        Else
            'If user input invalid type of conversion, jump to repeat label to try again
            Console.WriteLine("Invalid response, please try again")
            GoTo repeat
        End If

        'Ask user if they wish to try another conversion, read into retry variable
        Console.WriteLine("Would you like to try another conversion? (Y/N)")
        retry = UCase(Console.ReadLine)

        'If user chooses to try another, jump to repeat label, else continue
        If retry = "Y" Then
            GoTo repeat
        End If

        'Display exit message, console window closes after 1.5 seconds
        Console.WriteLine("Thank you for using the converter.")
        System.Threading.Thread.Sleep(1500)
        Console.Out.Close()

    End Sub

    Sub BIN()

        'Prompt user to input value to be converted, value converted to denary as it is read into variable 'output1'
        Console.WriteLine("Please enter the Binary number you wish to convert to Denary")
        Dim output1 As Integer = Convert.ToInt32(Console.ReadLine, 2)
        'Result of conversion is outputted to user
        Console.WriteLine("Your number in Denary is " & output1)
    End Sub

    Sub DEN()

        'Prompt user to input value to be converted, value converted to binary as it is read into variable 'output1'
        Console.WriteLine("Please enter the Denary number you wish to convert to Binary")
        Dim output1 As Integer = Convert.ToInt32(Console.ReadLine, 10)
        'Result of conversion is outputted to user
        Console.WriteLine("your number in Binary is " & output1)

    End Sub
End Module
 
Last edited:
Take the string input and convert to numeric value, then convert that to 2-base string.
VB.NET:
Dim input As String = "123"
Dim number As Integer
If Integer.TryParse(input, number) Then
    Dim binary As String = Convert.ToString(number, 2)

End If
Use a loop instead of GoTo statement.
GoTo statements can make code difficult to read and maintain. Whenever possible, use a control structure instead.
 
Back
Top