Convert textbox input to hexadecimal number
-
Thursday, February 10, 2011 4:15 AM
I try to find answer for my question in the previous thread but i can't found it.
my problem is:
i want to convert a string entered by a user from textbox to hexadecimal number.
the string is the hexadecimal code ( 0 - F )
for example if the user entered "12345678"
the string entered will be represented in an array like this:
array(0) = &H12
array(1) = &H34
array(2) = &H56
array(3) = &H78
and also if the input is like this : "1234"
the arrangement of the array is:
array(0) = &H01
array(1) = &H02
array(2) = &H03
array(3) = &H04
hopefully my question is clear.
thank you.
All Replies
-
Thursday, February 10, 2011 7:07 AM
Hello Red,
I've just programmed a Hex Editor. Here is my code. Part1: Enter string n Hexformat only 0 ..9, A..F into a Box
Part2: convert this string to byte array. This also works with a TextBox
sucess and best regards Ellen
Private count As Integer ' ' enter text in Format 1F AA 01 A1 e3 .......... ' Private Sub ComboBox1_TextUpdate(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cbxEnterText.TextUpdate If Not txEnterHex.Checked Then Exit Sub Dim cb As ToolStripComboBox = CType(sender, ToolStripComboBox) count += 1 If count = 2 Then cb.Text &= " " count = 0 End If End Sub ' ' only allowed hex keys ' Private Sub ComboBox1_KeyPress(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.KeyPressEventArgs) _ Handles cbxEnterText.KeyPress Dim allowedChars As String = "0123456789ABCDEFabcdef" Dim found As Boolean If e.KeyChar <> keys.Enter Then If txEnterHex.Checked Then For i As Integer = 1 To allowedChars.Length If e.KeyChar = GetChar(allowedChars, i) Then found = True Exit For End If Next If Not found Then e.Handled = True End If End If Else count = 0 ' ' here code to convert the line to hex array ' reconvert(cbxEnterText.Text) End If End Sub ' ' convert string 1F AA 01 A1 e3 .......... to binary array ' Private Function reconvert(ByVal t As String) As Byte() Dim data() As Byte = Nothing Dim s() As String = t.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries) data = New Byte(s.Length - 1) {} For i As Integer = 0 To data.Length - 1 If Not Byte.TryParse(s(i), NumberStyles.HexNumber, CultureInfo.CurrentCulture, data(i)) Then data(i) = 255 MsgBox("error converting!", MsgBoxStyle.Information) End If Next Return data End Function
Ich benutze/ I'm using VB2008 & VB2010- Marked As Answer by Red 02 Friday, February 11, 2011 12:42 AM
-
Thursday, February 10, 2011 8:44 AM
Hi Red,
You can do things difficult and do things normal.
Hex is not a kind of value or whatever, it is a representation of a value.
10 is represented in Hex notation A while it is in octal 12
Internal it is just the same it are the bits
1010
Be aware that this is not equivalent to a code system where in Net is currently mostly used for numbers a code which is for all code tables the same.
The written number 1 is in that equal to code 49 which is in hexadecimal notation 31 which has to do with the bits because it is just meaning
0010001
For conversion in this are in VB the conversion methods
for instance Hex, ASC, ASCW, Chrw, Chr
http://msdn.microsoft.com/en-us/library/1bbh5ae4(v=VS.100).aspx
Success
Cor -
Friday, February 11, 2011 12:43 AMthank you for the quick reply and the answer is really2 helpful..
-
Friday, February 18, 2011 3:55 AM
hi ellen, i got problem with your code. sorry because i'm really at the beginner stage of exploring vb.net.
fisrtly i do not understand your withevent code of
Private Sub ComboBox1_TextUpdate(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _ Handles cbxEnterText.TextUpdate
where does the cbxentertext come from?
-
Friday, February 18, 2011 7:48 PM
hi ellen, i got problem with your code. sorry because i'm really at the beginner stage of exploring vb.net.
fisrtly i do not understand your withevent code of
Private Sub ComboBox1_TextUpdate(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _ Handles cbxEnterText.TextUpdate
where does the cbxentertext come from?
Hello red,
sorry for my delayed reply. What You see here is special feature (or bug ... ) in Visual Studio. When I design a Combobox with Form Designer the default name is ComboBox1. I usually then rename the identifieres here to cbxententerText. But this will not automatically change the name of the event routine. here remains the name ComboBox1_TextDown. But this is no issue. Try to rename this to any name. You will see the code runs. The reason is: The Sub will be executed by the Handle and not by the method name.
best regards Ellen
Ich benutze/ I'm using VB2008 & VB2010

