Link to home
Start Free TrialLog in
Avatar of indy500fan
indy500fan

asked on

Converting ASCII to HEX

Friends,

I am recieving a serial stream of characters in ASCII, and once I have a valid string of these ASCII characters, I need to convert them to HEX.

I have created a sample app, with a sample string, but it doesn't work.  It only converts the First ASCII character to HEX, and ignores the rest.

What do I need to do to convert all of the characters?

Here is my sample app:

Public Class AYFSMain

    Dim DelphiString As String = "ñ]¬ªªUUwwE"
    Dim DelphiStringHex As String = Hex(Asc(DelphiString))

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles  Button1.Click
        MessageBox.Show(DelphiStringHex, "Result", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub
End Class
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

       Dim DelphiString As String = "ñ]¬ªªUUwwE"
        Dim DelphiStringHex As String

        Dim x As Integer
        For x = 1 To Len(DelphiString - 1)
            DelphiStringHex = DelphiStringHex & Hex(Asc(DelphiString.Substring(x, 1)))
        Next
        MsgBox(DelphiStringHex)
ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of indy500fan
indy500fan

ASKER

That works with one little tweak.

I needed to change For x = 1 To Len... to For x = 0.  Other than that, PERFECT!

Thanks!
Yes, I miss that!

:-)
Just another way...

Dim DelphiString As String = "ñ]¬ªªUUwwE"
Dim ConvertedToHex As String = ""

For Each MyCharArray As Char In DelphiString.ToCharArray()
     ConvertedToHex += Hex(Asc(MyCharArray))
Next

MessageBox.Show(ConvertedToHex, "Result", MessageBoxButtons.OK, MessageBoxIcon.Information)
DarrenD,

Hey, that's pretty slick!

Thanks,
Eric