Binary to decimal conversion in vb.net

Convert binary number into decimal number

Vb program for Binary to decimal conversion. Here mentioned other language solution.

' Include namespace system
Imports System 
'  Vb.net program for
'  Convert binary to decimal number
public Class DecimalNumber
    Public Sub binaryTodecimal(ByVal number As String)
        '  Assuming that number contains 0,1s
        '  Used to store result
        Dim result As Long = 0
        Dim bit As Integer = 0
        Dim n As Integer = number.Length - 1
        '  Display Binary number
        Console.Write("Binary :  " + number)
        '  Execute given number in reverse order
        while (n >= 0)
            if (number(n) = "1"c) Then
                '  When get binary 1
                result += (1 << (bit))
            End If
            n = n - 1
            '  Count number of bits
            bit += 1
        End While
        '  Display decimal result
        Console.WriteLine("  Decimal :  {0}", result)
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim task As DecimalNumber = New DecimalNumber()
        '  Test Cases
        task.binaryTodecimal("1111")
        task.binaryTodecimal("10111")
        task.binaryTodecimal("10101")
        task.binaryTodecimal("1001")
        task.binaryTodecimal("1101")
    End Sub
End Class

Output

Binary :  1111  Decimal :  15
Binary :  10111  Decimal :  23
Binary :  10101  Decimal :  21
Binary :  1001  Decimal :  9
Binary :  1101  Decimal :  13


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved