?
Solved

Converting ASCII to HEX

Posted on 2007-08-01
6
Medium Priority
?
365 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:indy500fan
  • 3
  • 2
6 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 19608328
       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)
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 2000 total points
ID: 19608329
Sorry
       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)
0
 

Author Comment

by:indy500fan
ID: 19608351
That works with one little tweak.

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

Thanks!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:jpaulino
ID: 19608388
Yes, I miss that!

:-)
0
 
LVL 18

Expert Comment

by:DarrenD
ID: 19608399
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)
0
 

Author Comment

by:indy500fan
ID: 19608431
DarrenD,

Hey, that's pretty slick!

Thanks,
Eric
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question