Click to See Complete Forum and Search --> : Binary numer


udipr
June 17th, 2001, 04:53 AM
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

cksiow
June 17th, 2001, 07:58 PM
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

Iouri
June 18th, 2001, 07:23 AM
'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
iouri@hotsheet.com