Script Library
Function fBin2Dec(strBin)
Search thecollinshouse script library:
Script Type: vbScript function
Description:
This function converts a binary number string into a decimal integer. Tested on Windows XP.
Script:

'# D.Collins - 12:03 21/01/2004
'# Convert Binary number string to Decimal

MsgBox "Binary 1100 = Decimal " & fBin2Dec("1100")

Function fBin2Dec(strBin)
    Dim intDecout, intBits
    intDecout = 0
    intBits = Len(strBin)
    
    'convert binary to decimal
    For i = 1 To intBits
        If Mid(strBin, i, 1) = "1" Then
            intDecout = intDecout + (2 ^ intBits) / 2
        Elseif Mid(strBin, i, 1) <> "0" Then
            MsgBox "Invalid input", , "Error"
            intDecout = -1
            Exit Function
        End If
        intBits = intBits - 1
    Next
    fBin2Dec = CInt(intDecout)
End Function

Script Library Index | thecollinshouse Home
14737