Added: Aug 19, 2002
Snippet uploaded by: snippet
Written By: Unknown
Demo: Sorry, no demo
Converts to Decimal to Binary and Binary to Decimal.
Highlight all by clicking in box
Dim decml As Double
Dim jbinary As String
Dim digit As Single, power As Single
Dim h As Integer, i As Integer, jbin As Integer
Dim j As Integer, k As Integer
Private Sub cmdCalculate_Click()
decml = 16 'this is the number to convert
DecBin decml, jbin ' convert from decimal to binary
Print "Decimal ="; BinDec(jbin) ' convert from binary to decimal
Print "binary = "; jbin ' display the result
Print "Hex = "; Hex(decml)
Print "OCTAL = "; Oct$(decml)
' converts a string of binary digits to its decimal equivalent
Static Function BinDec(jbinary)
Dim digit, power, i, jbin As Integer
decml = 0: jbin = 0
jbinary = UCase(jbinary)
For i = Len(jbinary) To 1 step -1 ' convert from binary to decimal
digit = Asc(Mid(jbinary, i, 1)) - 48
If digit < 0 Or digit > 1 Then decml = 0: Exit For
decml = decml + digit * 2 ^ (power)
power = power + 1
BinDec = decml
' converts a decimal value to an equivalent string of binary digits
Static Sub DecBin(decml, jbin)
jbin = ""
h = Hex(decml) ' convert from decimal to hexadecimal
For i = 1 To Len(h)
digit = InStr("0123456789ABCDEF", Mid(h, i, 1)) - 1
If digit < 0 Then jbin = "": Exit For
j = 8: k = 4
Do ' convert from hexadecimal to binary
jbin = jbin + Right$(Str((digit j) Mod 2), 1)
j = j - (j 2): k = k - 1
If k = 0 Then Exit Do
Loop While j
No Comments to show
Please completely fill out the form below if you want to review this snippet. All reviews are subject to validation.
Adding your comment. Please wait...
Thanks for adding your comment!. After further review it will be added.
There was a problem adding your comment. Please try again.
Please complete all the fields in the form before sending.