decimal to binary conversion in VB6


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: decimal to binary conversion in VB6

  1. #1
    tony Guest

    decimal to binary conversion in VB6


    Does anybody know a way in VB to convert a decimal number to its corresponding
    binary format? For example, if you open the windows calculator and switch
    to scientific mode and type in the following number (in dec format) '12345',
    then click the bin (binary) format, you will get '11000000111001' in response.

    If I were to simply take each digit out of the number and convert them seperately,
    I would not come up with the same result. To convert each digit seperately,
    I would get:
    0001 0010 0011 0100 0110

    As you can tell, this is not the same result as converting the entire number.

    Any help would be greatly appreciated.

  2. #2
    ralph Guest

    Re: decimal to binary conversion in VB6


    "tony" <anthonygrosso@hotmail.com> wrote:
    >
    >Does anybody know a way in VB to convert a decimal number to its corresponding
    >binary format? For example, if you open the windows calculator and switch
    >to scientific mode and type in the following number (in dec format) '12345',
    >then click the bin (binary) format, you will get '11000000111001' in response.
    >
    >If I were to simply take each digit out of the number and convert them seperately,
    >I would not come up with the same result. To convert each digit seperately,
    >I would get:
    >0001 0010 0011 0100 0110
    >
    >As you can tell, this is not the same result as converting the entire number.
    >
    >Any help would be greatly appreciated.



    Public Function LongToBin(ByVal Nbr As Long) As String
    On Error GoTo ErrorHandler
    Dim lBase As Long
    Dim sTmp As String

    lBase = (2 ^ 31) - 1
    While lBase > 0
    If Nbr - lBase >= 0 Then
    Nbr = Nbr - lBase
    sTmp = sTmp & "1"
    Else
    If Val(sTmp) > 0 Then sTmp = sTmp & "0"
    End If
    lBase = lBase / 2
    Wend
    LongToBin = sTmp
    Exit Function
    ErrorHandler:
    LongToBin = "0"
    End Function


  3. #3
    Edmund Guest

    Re: decimal to binary conversion in VB6


    "tony" <anthonygrosso@hotmail.com> wrote:
    >
    >Does anybody know a way in VB to convert a decimal number to its corresponding
    >binary format? For example, if you open the windows calculator and switch
    >to scientific mode and type in the following number (in dec format) '12345',
    >then click the bin (binary) format, you will get '11000000111001' in response.
    >
    >If I were to simply take each digit out of the number and convert them seperately,
    >I would not come up with the same result. To convert each digit seperately,
    >I would get:
    >0001 0010 0011 0100 0110
    >
    >As you can tell, this is not the same result as converting the entire number.
    >
    >Any help would be greatly appreciated.


    Public Function NumToBin(nmr as Variant) As String

    Do Until nmr <= 1
    NumToBin = Trim(Str(nmr Mod 2)) & NumToBin
    nmr = Int(nmr / 2)
    Loop
    If nmr = 1 then NumToBin = "1" & NumToBin

    NumToBin = Right("00000000" & NumToBin, 8)

    End Function

    This is setup for 8 bit bytes - anything different would have to have different
    padding.

    You could change this to other number bases (less than 10) by changing the
    "2". Bases greater than ten would have to substitute letters when greater
    than 9 in the Do - Loop.



  4. #4
    Rick Rothstein Guest

    Re: decimal to binary conversion in VB6

    "tony" <anthonygrosso@hotmail.com> wrote in message
    news:3e37c8b2$1@tnews.web.devx.com...
    >
    > Does anybody know a way in VB to convert a decimal number to its

    corresponding
    > binary format? For example, if you open the windows calculator and

    switch
    > to scientific mode and type in the following number (in dec format)

    '12345',
    > then click the bin (binary) format, you will get '11000000111001' in

    response.
    >
    > If I were to simply take each digit out of the number and convert them

    seperately,
    > I would not come up with the same result. To convert each digit

    seperately,
    > I would get:
    > 0001 0010 0011 0100 0110
    >
    > As you can tell, this is not the same result as converting the entire

    number.
    >
    > Any help would be greatly appreciated.


    These two functions will work with numbers up to 96 bits long. (Use
    Variant variables for the Decimal Value holders if your string is over
    32 bits).

    Rick - MVP

    'Decimal To Binary
    ' =================
    ' NOTE: You can limit the size of the returned
    ' answer by specifying the number of bits
    Function Dec2Bin(ByVal DecimalIn As Variant, _
    Optional NumberOfBits As Variant) As String
    Dec2Bin = ""
    DecimalIn = Int(CDec(DecimalIn))
    Do While DecimalIn <> 0
    Dec2Bin = Trim$(Str$(DecimalIn - _
    2 * Int(DecimalIn / 2))) & Dec2Bin
    DecimalIn = Int(DecimalIn / 2)
    Loop
    If Not IsMissing(NumberOfBits) Then
    If Len(Dec2Bin) > NumberOfBits Then
    Dec2Bin = "Error - Number exceeds specified bit size"
    Else
    Dec2Bin = Right$(String$(NumberOfBits, _
    "0") & Dec2Bin, NumberOfBits)
    End If
    End If
    End Function


    'Binary To Decimal
    ' =================
    Function Bin2Dec(BinaryString As String) As Variant
    Dim X As Integer
    For X = 0 To Len(BinaryString) - 1
    Bin2Dec = CDec(Bin2Dec) + Val(Mid(BinaryString, _
    Len(BinaryString) - X, 1)) * 2 ^ X
    Next
    End Function



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center