VB Converter for Decimal and Binary Numbers

Feedback

Question:

Uncertainty looms over the equation that vb will accept for my code to function. The user is given the option to input either a binary or decimal number, which will subsequently be converted using an appropriate equation. Although I have begun the decimal conversion process, I have become stuck in deciding which equation to employ.

This is the code:

Module Module1
Sub Main()
    Dim userchoice As String
    Dim numberchoice, numchoice, newnumber As Integer
    Console.WriteLine("Please choose weather to convert 1) decimal to Binary or 2)Binary to decimal.")
    userchoice = Console.ReadLine()
    If userchoice = 1 Then
        Console.WriteLine("You are converting from a Decimal - Binary")
        Console.WriteLine("Please enter the number,")
        numberchoice = Console.ReadLine()
    End If
    If numberchoice > 128 Then
        Console.WriteLine("1")
        newnumber = numberchoice - 128
    End If
    If newnumber > 64 Then
        Console.WriteLine("1")
        newnumber = newnumber - 64
    End If
    If numberchoice > 32 Then
        Console.WriteLine("1")
        newnumber = newnumber - 32
    End If
    If numberchoice > 16 Then
        Console.WriteLine("1")
        newnumber = newnumber - 16
    End If
    If numberchoice > 8 Then
        Console.WriteLine("1")
        newnumber = newnumber - 8
    End If
    If numberchoice > 4 Then
        Console.WriteLine("1")
        newnumber = newnumber - 4
    End If
    If numberchoice > 2 Then
        Console.WriteLine("1")
        newnumber = newnumber - 2
    End If
    If numberchoice > 1 Then
        Console.WriteLine("1")
        newnumber = newnumber - 1
    End If
    Console.ReadLine()
    If userchoice = 2 Then
        Console.WriteLine("You're converting from Binary - Decimal")
        Console.WriteLine("Please enter the binary number,")
        numchoice = Console.ReadLine()
    End If
End Sub
End Module



Solution:

This is what you seek:

Module Module1
Sub Main()
    While True
        Console.WriteLine("Please choose whether to convert:")
        Console.WriteLine("1.) Decimal to Binary")
        Console.WriteLine("2.) Binary to Decimal")
        Dim userChoice As String = Console.ReadLine()
        If userChoice = 1 Then
            Console.WriteLine("Converting from Decimal to Binary.")
            Console.Write("Please enter a Decimal number: ")
            Dim decimalNumber = Console.ReadLine()
            Console.WriteLine(String.Format("Binary representation of {0} in Decimal is {1}.", decimalNumber, ToBinary(decimalNumber)))
        ElseIf userChoice = 2 Then
            Console.WriteLine("Converting from Binary to Decimal.")
            Console.Write("Please enter a Binary number: ")
            Dim binaryNumber = Console.ReadLine()
            Console.WriteLine(String.Format("Decimal representation of {0} in Binary is {1}.", binaryNumber, ToDecimal(binaryNumber)))
        Else
            Console.WriteLine("Please enter a correct value.")
        End If
        Console.WriteLine(Environment.NewLine + "-------------------------------" + Environment.NewLine)
    End While
End Sub
Function ToBinary(input As String) As String
    Return Convert.ToString(Int64.Parse(input), 2)
End Function
Function ToDecimal(input As String) As String
    Return Convert.ToString(Int64.Parse(input), 10)
End Function
End Module

Frequently Asked Questions

Posted in Vba