Hex to Ascii Conversion


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?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
>>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
adam_pedleyAuthor Commented:
Then do you know why it doesnt go through on the conversion?

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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

Oops, sorry, somehow missed zzzzzooc's comment - he already explained all
adam_pedleyAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.