• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 429
  • Last Modified:

Hex to Ascii Conversion

Hi

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

Thanks
Adam
0
adam_pedley
Asked:
adam_pedley
  • 3
  • 2
2 Solutions
 
zzzzzoocCommented:
>>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
0
 
ArkCommented:
Hi
>>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
0
 
adam_pedleyAuthor Commented:
Then do you know why it doesnt go through on the conversion?

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!

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

Regards
0
 
ArkCommented:
Oops, sorry, somehow missed zzzzzooc's comment - he already explained all
0
 
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.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now