CodeItBetter Programming Another VB Programming Blog

How to Convert Binary to Decimal

Posted on August 14, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
'Coding Basics - How to Convert Binary to Decimal
Public Function BinToDec(value As String) As Long
    Dim result As Long, i As Integer, exponent As Integer
    For i = Len(value) To 1 Step -1
        Select Case Asc(Mid$(value, i, 1))
            Case 48      ' "0", do nothing.
            Case 49      ' "1", add the corresponding power of 2.
                result = result + Power2(exponent)
            Case Else
                Err.Raise 5  ' Invalid procedure call or argument
        End Select
        exponent = exponent + 1
    Next
    BinToDec = result
End Function
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.