CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Binary numer

  1. #1
    Join Date
    Dec 2000
    Posts
    163

    Binary numer

    Hi

    I know that if I want to write (in code) number in HEX I need to write like that:
    X = &HFF40


    How can I write number in BINARY ???

    Thanks


  2. #2
    Join Date
    Apr 2000
    Posts
    737

    Re: Binary numer

    I am not sure whether VB support BINARY number natively, I doubt it. However, you can write you own function like this

    Dec2Bin, Bin2Dec.

    which convert integer value to binary string and vice versa, if u need example, refer http://vblib.virtualave.net, vbConversion of the activeX DLL.

    HTH


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

    Re: Binary numer

    'convert any number to binary

    private Sub cmdNumber_Click() 'This sub converts the number to binary
    Dim lInput as Double
    Dim sOutput as string
    Dim iX as Integer

    If len(txtNumber) > 0 then 'if a valid number exists
    lInput = CLng(txtNumber.Text)
    sOutput = "" 'reset output string
    While lInput > 0
    iX = lInput Mod 2 'return the modulus
    sOutput = iX & sOutput 'append result to output string
    lInput = Int(lInput / 2) 'return the next number for calculation
    Wend
    While len(sOutput) < 8 'add leading zeros if the result is shorter than 8 characters
    sOutput = 0 & sOutput
    Wend
    MsgBox "The binary eqivalent of the number " & txtNumber.Text & " is:" & vbLf & vbLf & sOutput, vbInformation + vbOKOnly, "Success" 'display result
    txtNumber.Text = "" 'reset text box
    else 'if no valid number exists
    MsgBox "Please enter a number.", vbCritical + vbOKOnly, "error"
    End If
    txtNumber.SetFocus 'reset focus to textbox
    End Sub

    private Sub txtNumber_Change()
    Dim X as Integer

    If IsNumeric(txtNumber.Text) = false then 'if latest character isn't a number
    If len(txtNumber.Text) > 1 then
    txtNumber.Text = Left(txtNumber.Text, len(txtNumber.Text) - 1) 'remove the last caracter from the string
    else
    txtNumber.Text = ""
    End If
    else
    If InStr(1, txtNumber.Text, ".", vbTextCompare) > 0 then 'remove any periods
    txtNumber.Text = Left(txtNumber.Text, len(txtNumber.Text) - 1)
    ElseIf InStr(1, txtNumber.Text, "+", vbTextCompare) > 0 then 'remove any plus signs
    txtNumber.Text = Left(txtNumber.Text, len(txtNumber.Text) - 1)
    ElseIf InStr(1, txtNumber.Text, "-", vbTextCompare) > 0 then 'remove any minus signs
    txtNumber.Text = Left(txtNumber.Text, len(txtNumber.Text) - 1)
    ElseIf InStr(1, txtNumber.Text, "$", vbTextCompare) > 0 then 'remove any dollar signs
    txtNumber.Text = Left(txtNumber.Text, len(txtNumber.Text) - 1)
    ElseIf InStr(1, txtNumber.Text, ",", vbTextCompare) > 0 then 'remove any commas
    txtNumber.Text = Left(txtNumber.Text, len(txtNumber.Text) - 1)
    End If
    End If
    txtNumber.SelStart = len(txtNumber.Text) 'reset the cursor position
    End Sub




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

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