Write a VBA code to convert an arbitrary decimal integer number to equivalent bi
ID: 3873915 • Letter: W
Question
Write a VBA code to convert an arbitrary decimal integer number to equivalent binary number.
Specific requirements:
1. Use InputBox function to request the input for an arbitrary decimal integer;
2. Convert the decimal integer to equivalent binary number;
3. Return the binary number in a message box.
Below functions and symbol may be useful in your VBA code:
Cstr( ): can convert a number in ( ) to a string;
Int( ): can trim off fractional part and just return integer part of the number in ( ); & : can combine multiple
Explanation / Answer
The function DecToBin takes Integer as input and returns its equivalent binary string as output
Public Function DecToBin(ByVal lngDec As Long) As String
Const MAXLEN = 30
Dim strBin As String
Dim n As Long
If lngDec < 0 Then
strBin = "1"
Else
strBin = "0"
End If
For n = MAXLEN To 0 Step -1
If lngDec And (2 ^ n) Then
strBin = strBin & "1"
Else
strBin = strBin & "0"
End If
Next
DecToBin = strBin
End Function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.