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
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





Fri, 08 Oct 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. converting from 16 bit binary to decimal (base 10)

2. Convert numbers from base 36 to base 10?

3. I need help converting base from binary to decimal

4. Convert Decimal to Binary,Binary to Decimal VB4

5. HOW to convert from Decimal to 256 Base

6. Help: converting VB3 binary text based program to to VB5 database

7. hex to base-10

8. Base 10 logs

9. help: need to convert decimal numbers into binary

10. Need basic program for converting bases.

11. Binary to Decimal Algorithm

12. Base 36 Algorithm

 

 
Powered by phpBB® Forum Software