+ Reply to Thread
Results 1 to 4 of 4

convert raw hex string to ASCII

Hybrid View

  1. #1
    Registered User
    Join Date
    06-04-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    3

    convert raw hex string to ASCII

    hello,
    I've been wrestling with this for a while now and need some help.

    I am making a system call to read a serial port as follows:
    status = CommRead(Port, Buffer, Size)

    Where Port is an integer, Buffer is a String and Size is a Long

    I get a string of 20 raw hex values from the device I am reading.
    When I put the Buffer String into a cell, only the numbers that are ASCII characters are displayed.
    I need to convert the raw hex value to an ASCII set then place the 20 values into 20 different rows.

    I've tried indexing into the string, but I always seem to get compile errors. Buffer.Chars(int)
    Side note, my version of VB won't let me declare a Char variable.

    Thanks for any help you can offer.
    Last edited by playitloud; 06-10-2013 at 11:43 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: convert raw hex string to ASCII

    You probably need a FOR/NEXT loop to step through and break down your input string into pairs of bytes which are presumably numerical.

    e.g
    For X = 1 to Len(Buffer) step 2
    Range("A" & X).value = mid(Buffer,X,2)
    Next x
    Elegant Simplicity............. Not Always

  3. #3
    Registered User
    Join Date
    06-04-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: convert raw hex string to ASCII

    Quote Originally Posted by AndyLitch View Post
    You probably need a FOR/NEXT loop to step through and break down your input string into pairs of bytes which are presumably numerical.

    e.g
    For X = 1 to Len(Buffer) step 2
    Range("A" & X).value = mid(Buffer,X,2)
    Next x
    I see what your code is doing, but the string I get back is not ASCII hex, it needs to be converted from the raw value to something that can be displayed in a cell.

  4. #4
    Registered User
    Join Date
    06-04-2013
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: convert raw hex string to ASCII

    Well, after wrestling around with this a bit more I finally figured it out.
    What are the rules for posting code here?


        Dim intPortID As Integer
        Dim lngStatus As Long
        Dim strDatain As String
        Dim x As Long, endhere As Long
        Dim singlechar As String
                
        intPortID = Range("A4").Value
        lngStatus = CommRead(intPortID, strDatain, 20)
        endhere = Len(strDatain)
        For x = 1 To endhere
            singlechar = Asc(Mid(strDatain, x, 1))
            Range("H" & x).Value = singlechar
        Next
        Range("G9").Value = strDatain
        Range("F9").Value = lngStatus
        Range("F10").Value = Len(strDatain)
    Last edited by playitloud; 06-10-2013 at 11:06 AM. Reason: added code

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1