Hex to Ascii Conversion

Posted on 2004-11-11
Last Modified: 2013-12-25

I have to convert an ASCII hex string eg "0100BBFF060A" to actual hex to send out of the serial port

I can do this via this function below
            'Declare Variables
            Dim i As Integer
            Dim strTemp As String
            Dim ConvertedValue As String
            'For each hex value do
            For i = 1 To (Len(value)) Step 2
                'Get the next hex value
                strTemp = Mid$(value, i, 2)
                'Convert the hex value to ascii and append it to the string
                ConvertedValue = ConvertedValue & Chr(CInt(Val("&H" & strTemp)))
                'hex strings are 2 characters thats why 2 characters are taken at a time and why
            Return ConvertedValue

This works fine but if a hex value of "00" comes throught the ascii character for it is null, so nothing is added to the string.

Does anyone know how to get around this?

Question by:adam_pedley
    LVL 17

    Accepted Solution

    >>but if a hex value of "00" comes throught the ascii character for it is null, so nothing is added to the string.
    Null characters can be contained within strings fine within VB. They're used to terminate strings in other languages but VB uses BSTRs which store the length of the string so it does not need a terminating null-character to know when it ends.

    Private Sub Form_Load()
        'The Len() will equal 6 because 6 null-characters are in the string
        MsgBox Len(Hex2String("000000000000"))
    End Sub
    Private Function Hex2String(ByRef strHex As String) As String
        Dim intLoop As Integer, intTemp As Integer
        For intLoop = 1 To Len(strHex) Step 2
            Hex2String = Hex2String & Chr(Val("&H" & Mid(strHex, intLoop, 2)))
        Next intLoop
    End Function
    LVL 27

    Expert Comment

    >>This works fine but if a hex value of "00" comes throught the ascii character for it is null, so nothing is added to the string.<<
    Wrong. Chr(0) is NOT NULL - it's a special character
    LVL 4

    Author Comment

    Then do you know why it doesnt go through on the conversion?

    LVL 27

    Assisted Solution

    It does go through conversion - you can check this with Len(ConvertedValue). Chr(0) is unprintable character, used as EndOfANSIString mark, so you won't see it in a text box/label. But debug window replace Chr(0) with space:

    Private Sub Form_Load()
      Text1 = "a" & Chr(0) & "b"
      Label1 = "a" & Chr(0) & "b"
      Debug.Print "a" & Chr(0) & "b"
    End Sub

    LVL 27

    Expert Comment

    Oops, sorry, somehow missed zzzzzooc's comment - he already explained all
    LVL 4

    Author Comment

    I found out my problem in the end, its wasnt related to the chr(0) character it was the ascii com port driver in the backend. You couldnt set it to send in binary so the ascii characters were being seen as control characters. Anyway I found a work around for it (stop using the product :) ) This was citectVBA, stupid thing.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now