Need an Algorithm for Converting Decimals (Base 10) to Binary (base 2)

Charles,

Try this function. Pass it a long and it will return the binary version as

a string. I tried passing it back as a double (8 -byte), but large numbers

came back in E notation.

If you only plan on using smaller numbers, uncomment out the lines of code

that are commented in the If and Else statements and comment the lines that

are uncommented. You'll also need to do the same for the return value.

Just look at the comments in the code.

Let me know how this works for you.

--

Keith Benedict

Software Engineer

Triad Systems

** The reply address is a no spam address. To send to me

Function DecimalToBinary(lDec As Long) As String

Dim i As Long

Dim lAnd As Long

Dim dResult As Double

Dim szResult As String

Do

lAnd = 2 ^ i

If lAnd And lDec Then

'uncomment to return the number form

' lResult = lResult + (10 ^ i)

'comment to return the number form

szResult = "1" & szResult

Else

'comment to return the number form

szResult = "0" & szResult

End If

i = i + 1

Loop While lAnd < lDec

'comment to return the number form

DecimalToBinary = szResult

'uncomment to return the number form

DecimalToBinary = lResult

End Function

Quote:

> I am setting up a visual basic program for a user to input various

> ASCII charecters, and have it convert them to their Decimal equivalents,

> but I am "stumped" on getting the conversion of BASE 10 numbers to

> BINARY (Base 2).

> If you have an Algorithm or "Code" for this, can you send it to me, it

> would be appreciated.

> Thank you

> Charles Urlacher