窗外请进
给你一个2进制字串转十进制的VB算法,仅供参考。如有兴趣,请自行转化为SQL函数。
每次处理的字串最大长度为31位。
Function Bin2Dec(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
Case 49
Result = Result + Power2(Exponent)
Case Else
Err.Raise 5 'invalid procedure call or argument
End Select
Exponent = Exponent + 1
Next
Bin2Dec = Result
End Function
Function Power2(ByVal Exponent As Long) As Long
Static Result(0 To 31) As Long, i As Integer
If Result(0) = 0 Then
Result(0) = 1
For i = 1 To 30
Result(i) = Result(i - 1) * 2
Next
Result(31) = &H80000000
End If
Power2 = Result(Exponent)
End Function