Dim lngTemp As Long lngTemp =(lngTemp And &HFF) / &H100&
dim i as long i = i /2^8 '右移八位 i = i * 2^8 '左移八位
二进制转十进制 Public Function Dec(ByVal n As String) As Double Dim i As Long, j As Long, dot As Long, prefix As Long prefix = Sgn(n) If prefix = -1 Then n = Mid(n, 2) dot = InStr(n, ".") If dot = 0 Then dot = Len(n) - 1 Else n = Left(n, dot - 1) & Mid(n, dot + 1) dot = dot - 2 End If For i = dot To dot - Len(n) + 1 Step -1 j = j + 1 If Mid(n, j, 1) <> 0 Then Dec = Dec + 2 ^ i Next Dec = Dec * prefix End Function
Function bindec(x As String) As Double ' 2 TO 10 Dim a a = Split(x, ".") y = 0 For i = 1 To Len(a(0)) y = y + Mid(a(0), i, 1) * 2 ^ (Len(a(0)) - i) Next For i = 1 To Len(a(1)) y = y + Mid(a(1), i, 1) / 2 ^ i Next bindec = y End Function
正整数的右移8位:dim a as longa=fix(a/2^8)楼上 northwolves(野性的呼唤) 的二进制转成十进制应该成为楼主的首选,很好!
lngTemp =(lngTemp And &HFF) / &H100&
i = i * 2^8 '左移八位
Public Function Dec(ByVal n As String) As Double
Dim i As Long, j As Long, dot As Long, prefix As Long
prefix = Sgn(n)
If prefix = -1 Then n = Mid(n, 2)
dot = InStr(n, ".")
If dot = 0 Then
dot = Len(n) - 1
Else
n = Left(n, dot - 1) & Mid(n, dot + 1)
dot = dot - 2
End If
For i = dot To dot - Len(n) + 1 Step -1
j = j + 1
If Mid(n, j, 1) <> 0 Then Dec = Dec + 2 ^ i
Next
Dec = Dec * prefix
End Function
Dim a
a = Split(x, ".")
y = 0
For i = 1 To Len(a(0))
y = y + Mid(a(0), i, 1) * 2 ^ (Len(a(0)) - i)
Next
For i = 1 To Len(a(1))
y = y + Mid(a(1), i, 1) / 2 ^ i
Next
bindec = y
End Function