Hex and Ascii issue converting back and forth from text and ascii
-
I need help converting between hex, ascii and back. I want to type in an alpha numeric like "ABC123" and save as a HEX formatted like this "## ## ## ## ## ## ## ## ## ## ## ##" Then later read that stored value and return to the text of "ABC123" I am having trouble reading it and returning to text. Here is what I try.
value = value.Replace(" ", "")
While (zero) shorthex = value.Substring(0, 2) If shorthex = "00" Then value = value.Substring(2, value.Length - 2) Else zero = False End If i = i + 1 End While Dim b((value.Length / 2) - 1) As Byte Dim x As Integer = b.Length - 1 For i = value.Length - 2 To 0 Step -2 b(x) = value.Substring(i, 2) x = x - 1 Next value = Long.Parse(value, Globalization.NumberStyles.HexNumber) Dim test As String = System.Text.Encoding.ASCII.GetString(b)
When this is done test does not show the valid Text. Any suggestions?
I can't think of anything cool and nerdy to say.
-
I need help converting between hex, ascii and back. I want to type in an alpha numeric like "ABC123" and save as a HEX formatted like this "## ## ## ## ## ## ## ## ## ## ## ##" Then later read that stored value and return to the text of "ABC123" I am having trouble reading it and returning to text. Here is what I try.
value = value.Replace(" ", "")
While (zero) shorthex = value.Substring(0, 2) If shorthex = "00" Then value = value.Substring(2, value.Length - 2) Else zero = False End If i = i + 1 End While Dim b((value.Length / 2) - 1) As Byte Dim x As Integer = b.Length - 1 For i = value.Length - 2 To 0 Step -2 b(x) = value.Substring(i, 2) x = x - 1 Next value = Long.Parse(value, Globalization.NumberStyles.HexNumber) Dim test As String = System.Text.Encoding.ASCII.GetString(b)
When this is done test does not show the valid Text. Any suggestions?
I can't think of anything cool and nerdy to say.
Hi Cory, your question is not really clear to me. How would you turn a six-char string ABC123 into a 24-char string (ignoring spaces)?? Please provide a full example, and/or provide the code that does the expansion. Anyway, here are some comments on the code shown: 1. the code is not safe, what if value starts of with all zeroes in it? 2. Long.Parse is returning a Long, you shouldn't assign the result to value since that is a string. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
Hi Cory, your question is not really clear to me. How would you turn a six-char string ABC123 into a 24-char string (ignoring spaces)?? Please provide a full example, and/or provide the code that does the expansion. Anyway, here are some comments on the code shown: 1. the code is not safe, what if value starts of with all zeroes in it? 2. Long.Parse is returning a Long, you shouldn't assign the result to value since that is a string. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
I want to get string from textbox and write to RFID device formatted in HEX "## ## ## ## ## ## ## ## ## ## ## ##" so, my goal is to (i think) convert text into Ascii then into hex and then I can save into the format. Then I want to read from RFID and convert back to the Text. So I think I take formatted hex and convert to hex number and then to ascii number then to Text. I am getting screwed up on the steps. I think this will do the Write.
Dim i As Integer
Dim x As Integer
'Create array
Dim bytearray(23) As String
For i = 0 To bytearray.Length - 1
bytearray(i) = "0"
NextDim hex As String Dim text As String = "" 'Change Text into ASCII hex = Strings.Asc(TextBox1.Text) 'Change Ascii into HEx hex = Long.Parse(hex, Globalization.NumberStyles.HexNumber) x = 0 'Put hex into Format For i = hex.Length - 1 To 0 Step -1 bytearray((bytearray.Length - 1) - x) = hex.Substring(i, 1) x += 1 Next 'Create full formatted string to write to RFID If TextBox1.Text<> "" Then For i = 0 To bytearray.Length - 1 Step 2 text = text + bytearray(i) + bytearray(i + 1) + " " Next text = text.Substring(0, text.Length - 1)
If "f" is typed in text box then text variable should say "00 00 00 00 00 00 00 00 00 00 02 58" I need help converting that back to "f" I can get it back down to 258 which I think is the hex. I need to know how to convert back to Ascii and then Text. Any ideas? Text
I can't think of anything cool and nerdy to say.
-
I want to get string from textbox and write to RFID device formatted in HEX "## ## ## ## ## ## ## ## ## ## ## ##" so, my goal is to (i think) convert text into Ascii then into hex and then I can save into the format. Then I want to read from RFID and convert back to the Text. So I think I take formatted hex and convert to hex number and then to ascii number then to Text. I am getting screwed up on the steps. I think this will do the Write.
Dim i As Integer
Dim x As Integer
'Create array
Dim bytearray(23) As String
For i = 0 To bytearray.Length - 1
bytearray(i) = "0"
NextDim hex As String Dim text As String = "" 'Change Text into ASCII hex = Strings.Asc(TextBox1.Text) 'Change Ascii into HEx hex = Long.Parse(hex, Globalization.NumberStyles.HexNumber) x = 0 'Put hex into Format For i = hex.Length - 1 To 0 Step -1 bytearray((bytearray.Length - 1) - x) = hex.Substring(i, 1) x += 1 Next 'Create full formatted string to write to RFID If TextBox1.Text<> "" Then For i = 0 To bytearray.Length - 1 Step 2 text = text + bytearray(i) + bytearray(i + 1) + " " Next text = text.Substring(0, text.Length - 1)
If "f" is typed in text box then text variable should say "00 00 00 00 00 00 00 00 00 00 02 58" I need help converting that back to "f" I can get it back down to 258 which I think is the hex. I need to know how to convert back to Ascii and then Text. Any ideas? Text
I can't think of anything cool and nerdy to say.
Hi, AFAIK this is completely wrong. 1. it does not work at all for more than one character since Strings.Asc(string) only looks at the first character of the string. 2. the conversion for a single character does not work (well, it does convert, but in a nonsensical way) The simple example where original text is "f", it is an ASCII character with numeric value 102 in decimal, which is 66 in hex. Indeed the outcome of Strings.Asc(TextBox1.Text) is the number 102 ERROR1: you erroneously store that in the string variable named "hex" which now holds "102" ERROR2: then you parse, i.e. decode, it as if it were a hex number, hence the result is the number 258 since 1*256+0*16+2*1 equals 258. ERROR3: you again erroneously store that in the string variable named "hex" which now holds "258" and finally you do a lot of moving around to get some zeroes and spaces What you probably want is something along these lines:
Sub rfid2() Dim maxChars As Integer = 12 Dim Text As String = "ABC123 f" Console.WriteLine("Encoding """ & Text & """ in " & maxChars & " hex chars") Dim length As Integer = Text.Length Dim result As String = "" Dim i As Integer For i = 1 To maxChars - length result = "00 " & result Next For i = 0 To length - 1 Dim chr As Integer = Strings.Asc(text.Substring(i, 1)) result = result & chr.ToString("X2") & " " Next Console.WriteLine("result=" + result) End Sub
This generates
"00 00 41 42 43 31 32 33 20 20 20 66 "
where each of the characters is clearly visible in hex. :)Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
Hi, AFAIK this is completely wrong. 1. it does not work at all for more than one character since Strings.Asc(string) only looks at the first character of the string. 2. the conversion for a single character does not work (well, it does convert, but in a nonsensical way) The simple example where original text is "f", it is an ASCII character with numeric value 102 in decimal, which is 66 in hex. Indeed the outcome of Strings.Asc(TextBox1.Text) is the number 102 ERROR1: you erroneously store that in the string variable named "hex" which now holds "102" ERROR2: then you parse, i.e. decode, it as if it were a hex number, hence the result is the number 258 since 1*256+0*16+2*1 equals 258. ERROR3: you again erroneously store that in the string variable named "hex" which now holds "258" and finally you do a lot of moving around to get some zeroes and spaces What you probably want is something along these lines:
Sub rfid2() Dim maxChars As Integer = 12 Dim Text As String = "ABC123 f" Console.WriteLine("Encoding """ & Text & """ in " & maxChars & " hex chars") Dim length As Integer = Text.Length Dim result As String = "" Dim i As Integer For i = 1 To maxChars - length result = "00 " & result Next For i = 0 To length - 1 Dim chr As Integer = Strings.Asc(text.Substring(i, 1)) result = result & chr.ToString("X2") & " " Next Console.WriteLine("result=" + result) End Sub
This generates
"00 00 41 42 43 31 32 33 20 20 20 66 "
where each of the characters is clearly visible in hex. :)Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
Ok, that works, but how do I take the ASCII and display the text?
I can't think of anything cool and nerdy to say.
-
Ok, that works, but how do I take the ASCII and display the text?
I can't think of anything cool and nerdy to say.
Hi, To reverse the process, you need to perform the reverse of the same steps, in reverse order. AFAIK the reverse of Strings.Asc is Strings.Chr, and the reverse of int16.ToString("X2") is int16.Parse() where you will want the right NumberStyle. I leave the details for you to work out. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
Hi, To reverse the process, you need to perform the reverse of the same steps, in reverse order. AFAIK the reverse of Strings.Asc is Strings.Chr, and the reverse of int16.ToString("X2") is int16.Parse() where you will want the right NumberStyle. I leave the details for you to work out. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
Got it! Thank you.
I can't think of anything cool and nerdy to say.