[VB]十進位制/十六進位制/二進位制 間的轉換
阿新 • • 發佈:2019-01-10
PublicFunction DecimalToBinary(DecimalValue AsLong, MinimumDigits AsInteger)
AsString
' Returns a string containing the binary
' representation of a positive integer.
Dim result AsString
Dim ExtraDigitsNeeded AsInteger
' Make sure value is not negative.
DecimalValue =Abs(DecimalValue)
' Construct the binary value.
Do
result =CStr(DecimalValue Mod2) & result
DecimalValue = DecimalValue 2
LoopWhile DecimalValue >0
' Add leading zeros if needed.
ExtraDigitsNeeded = MinimumDigits -Len(result)
If ExtraDigitsNeeded >0Then
result =String(ExtraDigitsNeeded, "0") & result
EndIf
DecimalToBinary = result
End Function
AsString
' Returns a string containing the binary
' representation of a positive integer.
Dim result AsString
Dim ExtraDigitsNeeded AsInteger
' Make sure value is not negative.
DecimalValue =Abs(DecimalValue)
' Construct the binary value.
Do
result =CStr(DecimalValue Mod2) & result
DecimalValue = DecimalValue 2
LoopWhile DecimalValue >0
' Add leading zeros if needed.
ExtraDigitsNeeded = MinimumDigits -Len(result)
If ExtraDigitsNeeded >0Then
result =String(ExtraDigitsNeeded, "0") & result
EndIf
DecimalToBinary = result
End Function