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

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

Wed, 06 Oct 1999 03:00:00 GMT
Need an Algorithm for Converting Decimals (Base 10) to Binary (base 2)

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.

sounds like you want ASC(" "). look in help
----------------------------------------------------------
if you didn't get answer here try http://www.erols.com/wmingee/

Fri, 08 Oct 1999 03:00:00 GMT
Need an Algorithm for Converting Decimals (Base 10) to Binary (base 2)

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.

Being a former FORTH programmer, this is a natural to me. :)

Function binText\$ (ByVal value&)
Dim result\$
Do
result\$ = CStr(value& And 1) & result\$
value& = Int(value& / 2) And &H7FFFFFFF
'Or: value& = value& \ 2  'only if value>=0
Loop While value&
binText\$ = result\$
End Function

Gent (Ghent, Gand),
Belgium,
Europe,
3rd planet from the sun.

Fri, 08 Oct 1999 03:00:00 GMT
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

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

Fri, 08 Oct 1999 03:00:00 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages