Binary Converter to Decimal with .NET

11,787

Solution 1

Edit: See below for going the other way!! I misread.

Binary to Decimal:

Public Function ConvertFromBinary(ByVal input As String) As Integer
    Dim ret As Integer = 0
    Dim splitInput As Char() = input.ToCharArray
    Dim modifier As Integer = 1
    For i As Integer = splitInput.Length - 1 To 0 Step -1
        Dim thisChar As Integer = Val(splitInput(i))
        If thisChar = 1 Then
            ret += thisChar * modifier
        End If
        modifier *= 2
    Next

    Return ret
End Function

...And back again:

Easy answer?

Convert.ToString(input, 2) 

Since that's no fun, however:

Public Function ConvertToBinary(ByVal input As Integer) As String

    Dim ret As String = ""

    While input > 0
        ret &= input Mod 2
        input = input \ 2
    End While

    Return StrReverse(ret)
End Function

Solution 2

The hard way

Private Sub btnConvert_Click(sender As Object, e As EventArgs) Handles btnConvert.Click
    Dim pwrOf2 As Integer = 0
    Dim ans As Long = 0L
    'first check if the user input a number
    If Long.TryParse(tbxBin.Text, Nothing) Then
        'then look at each char in reverse
        For Each n As Char In tbxBin.Text.Reverse
            Select Case n
                Case Is = "0"c
                Case Is = "1"c
                    ans += 1L << pwrOf2
                Case Else
                    'error - input not in binary, only 1's and 0's 
                    Stop
            End Select
            pwrOf2 += 1
        Next
        lblAns.Text = ans.ToString("n0")
    Else
        'not a number
    End If
End Sub

Solution 3

just a suggestion, start by making a function like this:

    Public Function Bin_To_Dec(ByVal Bin As String)
        Dim dec As Double = Nothing
        Dim length As Integer = Len(Bin)
        Dim temp As Integer = Nothing
        Dim x As Integer = Nothing

        For x = 1 To length
            temp = Val(Mid(Bin, length, 1))
            length = length - 1
            If temp <> "0" Then
                dec += (2 ^ (x - 1))
            End If
        Next

        Return dec
    End Function

then call it with this:

txtZ.Text = Bin_To_Dec(txtX.Text)
Share:
11,787
user3200800
Author by

user3200800

Updated on June 14, 2022

Comments

  • user3200800
    user3200800 almost 2 years

    I'm started to get the hang of some programming basics but I'm still very new and inexperienced. I am having trouble with a new program I am coding.

    I want to have a program in which an 8-bit binary number is put in a textbox, a button is pressed, and the decimal value of the binary number is shown.

    Below is the code i have tried:

    Public Class Form1
    
    Private Sub btnConvert_Click(sender As Object, e As EventArgs) Handles btnConvert.Click
    
        Dim N As Integer
        N = Convert.ToDouble(tbxBin.Text)
        N = N Mod 2
        N = N \ 2
        lblAns.Text = Convert.ToString(N)
    
    End Sub
    End Class
    

    Sadly, when I run the program I only get a decimal number of 0 no matter what binary code I put in. I'm very confused because I was told the use Mod class. When I look online for a solution to my problem, I see techniques and various coding processes I've never seen before.

    Apparently with my code, I have to repeat what I typed for as many bits as the user is typing in, however, I'm confused as to how I go about that. Do I Dim new variables?

    Any and all help is greatly appreciated. Thank you.

  • dbasnett
    dbasnett over 10 years
    Didn't the OP want to go from text ("1001100") to a number?
  • Andrew Morton
    Andrew Morton over 6 years
    That's very strange looking VB.NET ;)