CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 1999
    Location
    California
    Posts
    264

    How can I convert a decimal number to a binary number?

    How can I convert a decimal number to a binary number? Do you know if there's the function I can find in VB?

    Thank you for your help!

    Best Regards,

    Kevin Shen
    Best Regards,

    Kevin Shen

  2. #2
    Join Date
    Dec 2000
    Posts
    163

    Re: How can I convert a decimal number to a binary number?

    I don't think there is any function in VB, But I wrote function that do it.
    Try it:

    private Function DecimalToBinary(decNumber as Long) as string
    Dim i as Long
    Dim binNumber as string
    binNumber = ""
    i = 1
    Do While decNumber > 0
    If ((decNumber \ i) Mod 2) = 1 then
    binNumber = "1" & binNumber
    decNumber = decNumber - i
    else
    binNumber = "0" & binNumber
    End If
    i = i * 2
    Loop
    DecimalToBinary = binNumber
    End Function




    I hope it help you.


  3. #3
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: How can I convert a decimal number to a binary number?

    Here's a function that will convert any decimal number less than 256 to binary:

    Function Decimal2Binary(TheNumber As Integer) As String
    Dim TheBinaryNumber As String
    Dim ThePosition As Integer
    Dim iWork As Integer
    TheBinaryNumber = "00000000"
    If TheNumber < 256 Then
    ThePosition = 128
    For iWork = 1 To 8
    If TheNumber >= ThePosition Then
    Mid$(TheBinaryNumber, iWork, 1) = "1"
    TheNumber = TheNumber - ThePosition
    End If
    ThePosition = ThePosition / 2
    Next
    End If
    Decimal2Binary = TheBinaryNumber
    End Function



    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  4. #4
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: How can I convert a decimal number to a binary number?

    here's an article that has a routine that will go from any base number to any other base number and back again. http://www.codeguru.com/vb/articles/2137.shtml

    hope it can help,

    john

    John Pirkey
    MCSD
    http://www.ShallowWaterSystems.com
    http://www.stlvbug.org
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured