You've probably seen several of these, but this one avoids the performance trap of using exponentiation. As far as I know it works without quirks or bugs:
Code:Option Explicit Function Bits2Long(ByVal Bits As String) As Long Dim L As Long, Bits32 As Long, I As Long L = Len(Bits) If 1 > L Or L > 32 Or Bits Like "*[!01]*" Then Err.Raise 5 If L = 32 Then Bits32 = 1 For I = Bits32 + 1 To L Bits2Long = 2 * Bits2Long + (AscW(Mid$(Bits, I, 1)) - 48) Next If Bits32 And AscW(Bits) = 49 Then Bits2Long = Bits2Long Or &H80000000 End Function Function Bits2LongV2(ByVal Bits As String) As Long 'Stripped down, no error checking, only 1- to 31-bit argument: Dim I As Long For I = 1 To Len(Bits) Bits2LongV2 = 2 * Bits2LongV2 + (AscW(Mid$(Bits, I, 1)) - 48) Next End Function Private Sub Try(ByVal Bits As String) On Error Resume Next Print Hex$(Bits2Long(Bits)); Tab(16); Bits2Long(Bits); Tab(32); Bits If Err Then Print "Invalid parameter:"; Tab(32); Bits End If End Sub Private Sub Try2(ByVal Bits As String) Print Hex$(Bits2LongV2(Bits)); Tab(16); Bits2LongV2(Bits); Tab(32); Bits End Sub Private Sub Form_Load() AutoRedraw = True Try "11111111111111111111111111111111" Try "01111111111111111111111111111111" Try "0" Try "2" Try "" Try "111111111111111111111111111111111" Try "10101010" Try2 "10101010" End Sub


Reply With Quote