|
-
Feb 15th, 2009, 08:01 AM
#1
Thread Starter
Member
convert ASCII to char
I have a string of ASCII in HEX. ( 5A65726f)
so first i split them into 2s >>>> 5A 65 72 6f
then i change the hex to integer
using these integer i convert them to char using chr()
it did the job great, i can see the correct word i want..but it give me an error saying " make sure it has the right format" ( error shown below)
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim hexnum As String
Dim converttohex As String
Dim hextonum As Integer
Dim chara As Char
hexnum = txtReceivedMessage.Text
For i = 1 To Len(hexnum) Step 2
converttohex = Mid(hexnum, i, 2)
hextonum = Convert.ToInt32(converttohex, 16) '<<<<< ERROR here
chara = Chr(hextonum)
TextBox1.Text += chara
Next i
please help...
i thought it was the for loop problem...since i will go up to 7 while len(hexnum) is 8 so it would loop again but there is nothing after 8th HEX?
so i tried For i = 1 To Len(hexnum)-1 Step 2 ...this time...textbox didnt show anything...+ the same error came up...
-
Feb 15th, 2009, 08:21 AM
#2
Re: convert ASCII to char
All arrays in .NET are zero-based, so your loop should go from 0 to hexnum.Length-1.
-
Feb 15th, 2009, 08:29 AM
#3
Thread Starter
Member
Re: convert ASCII to char
 Originally Posted by Atheist
All arrays in .NET are zero-based, so your loop should go from 0 to hexnum.Length-1.
right...
but for the Mid() function ...the arguement must be greater than 0
how should i do it now?
-
Feb 15th, 2009, 08:32 AM
#4
Re: convert ASCII to char
The Mid function is just kept for legacy support in VB.NET, use the strings Substring method instead.
-
Feb 15th, 2009, 08:42 AM
#5
Thread Starter
Member
Re: convert ASCII to char
ok ..after I looked at it in detail....i found the string my device send to me application has some invalid char hiding...
so i use string.trim()
DONE!
Thank you Atheist anyway~
Code:
Dim hexnum As String
Dim converttohex As String
Dim hextonum As Integer
Dim chara As Char
hexnum = txtReceivedMessage.Text.trim() <<<!!!
For i = 0 To Len(hexnum)-1 Step 2
converttohex = hexnum.Substring(i, 2)
hextonum = Convert.ToInt32(converttohex, 16)
chara = Chr(hextonum)
TextBox1.Text += chara
Next i
Last edited by husky_doggy; Feb 15th, 2009 at 09:18 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|