Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote