十进制转二进制的函数

Public Function DecimalToBinary(DecimalValue As Long, MinimumDigits As Integer)
As String

' Returns a string containing the binary
' representation of a positive integer.

Dim result As String
Dim ExtraDigitsNeeded As Integer

' Make sure value is not negative.
DecimalValue = Abs(DecimalValue)

' Construct the binary value.
Do
    result = CStr(DecimalValue Mod 2) & result
    DecimalValue = DecimalValue \ 2
Loop While DecimalValue > 0

' Add leading zeros if needed.

ExtraDigitsNeeded = MinimumDigits - Len(result)
If ExtraDigitsNeeded > 0 Then
    result = String(ExtraDigitsNeeded, "0") & result
End If

DecimalToBinary = result

End Function

posted @ 2004-06-09 22:37  Popcorn  阅读(4750)  评论(7)    收藏  举报