Dim temp As String
temp = ""
Do
If x Mod 2 Then
temp = "1" + temp
Else
temp = "0" + temp
End If
x = x \ 2
If x < 1 Then Exit Do
Loop
Bin = temp
End Function
Public Function Dec2Bin(ByVal decValue) As String
While decValue > 0
Dec2Bin = decValue Mod 2 & Dec2Bin
decValue = decValue \ 2
Wend
End Function
Public Function Bin2Dec(ByVal decValue) As Long
Dim c As Variant
For i = 1 To Len(decValue)
Bin2Dec = Bin2Dec + Mid(StrReverse(decValue), i, 1) * 2 ^ c
c = c + 1
Next
End Function
LSD 可以给个效率高的方法吗?
http://www.mndsoft.com/blog/article.asp?id=268
Public Function DecToBin(ByVal DecNumber As Currency) As String
On Error Goto DecToBin_Finally
Dim BinNumber As String
Dim i%
For i = 64 To 0 Step -1
If Int(DecNumber / (2 ^ i)) = 1 Then
BinNumber = BinNumber & "1"
DecNumber = DecNumber - (2 ^ i)
Else
If BinNumber <> "" Then
BinNumber = BinNumber & "0"
End If
End If
Next
DecToBin = BinNumber
DecToBin_Finally:
If Err <> 0 Or BinNumber = "" Then DecToBin = "-E-"
Exit Function
End Function
这个是mndsoft上的,估计效率再提高会比较麻烦
如果还要高精度的话效率只会更低 除非用外接