Question conversion help needed

softhard

Active member
Joined
Sep 29, 2012
Messages
35
Programming Experience
Beginner
Hello,
I have a textbox where user enters either 0 or 1. If he enters 0 then i would like to send two byte as 0, 0 and if user enters 1 then i should send FF, 0. Now, i am trying in this way but it is not working in the case of when user enters 1.

Valuestr = Chr((CLng("0" & TextBox5.Text) And 65280) / 256) & Chr(CLng("0" & TextBox5.Text) And 255)

Please help me to how can i achieve this.
 
First of all, given that a user can type anything into a TextBox, wouldn't it make more sense to use a CheckBox, RadioButtons, a NumericUpDown or a ComboBox? It seems to me that a TextBox is the worst possible choice.

As for the code, wouldn't it make more sense to just use an If or Select Case statement?
Dim bytes As Byte()

Select Case myTextBox.Text
    Case "0"
        bytes = New Byte() {0, 0}
    Case "1"
        bytes = New Byte(){&HFF, 0}
    Case Else
        'Error
End Select
 
If you specifically want your hex value in a string:

VB.NET:
        Dim ValueStr As String

        Select Case TextBox1.Text.Trim
            Case "0"
                ValueStr = Chr(0) & Chr(0)
            Case "1"
                ValueStr = Chr(&HFF) & Chr(0)
            Case Else
                ' Exception
        End Select

Regards.
 
Back
Top