Results 1 to 5 of 5

Thread: convert ASCII to char

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    42

    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...

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: convert ASCII to char

    All arrays in .NET are zero-based, so your loop should go from 0 to hexnum.Length-1.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    42

    Re: convert ASCII to char

    Quote 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?

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: convert ASCII to char

    The Mid function is just kept for legacy support in VB.NET, use the strings Substring method instead.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    42

    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
  •  



Click Here to Expand Forum to Full Width