-1

I need to create a code that converts any binary number (up to 8 digits) into the decimal counterpart.

So i have created most of the program but i have one problem, this is that i have used a ToCharArray to split the string of numbers entered into the individual elements of an array. But then i have to use them number for arithmetic, - but for that they need to be in an integer array.

 Dim array(7) As Integer
    Dim num As String
    Dim i As Integer = 0
    Dim x As Integer = 0
    Dim y As Integer = 1
    Dim dec As Integer = 0
    console.writeline("Enter an 8-Digit binary string")
    num = console.readline()
    num.ToCharArray(array)
    array.Reverse(array)
    For i = 0 To 7
        dec = dec + array(x) * 1 * y
        x = x + 1
        y = y * 2
    Next

    console.write(dec)
    console.read()

(Sorry i didn't know which parts would be helpful or not, so heres what ive got so far)

  • 1
    You should take a look at this question and its answer: https://stackoverflow.com/questions/1961599/how-to-convert-binary-to-decimal – JayV Jul 31 '19 at 13:52
  • Curious why you you take the suggestions offered you in your previous question? Dim dec As Decimal = 0 dec = Convert.ToInt64(1101, 2) – HereGoes Jul 31 '19 at 13:59
  • Please note that `.ToCharArray` is a function, so you have to assign the result of it to something or else there is no point in doing it. It's like writing "1 + 2" instead of "x = 1 + 2". – Andrew Morton Jul 31 '19 at 23:26

1 Answers1

1

Make your life easier and take advantage of the convert in vb.net so be something very simple like this

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim binary As String = "11010110"

        Console.WriteLine(ToDecimal(binary))
    End Sub

    Function ToDecimal(input As String) As Integer
        Dim i As Integer = Convert.ToInt32(input, 2)
        Return i
    End Function
K.Madden
  • 353
  • 1
  • 16
  • Please turn on Option Strict. This is a 2 part process. First for the current project - In Solution Explorer double click My Project. Choose Compile on the left. In the Option Strict drop-down select ON. Second for future projects - Go to the Tools Menu -> Options -> Projects and Solutions -> VB Defaults. In the Option Strict drop-down select ON. This will save you from bugs at runtime. – Mary Jul 31 '19 at 22:28