Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2003
    Posts
    27

    Unanswered: hex2dec conversion using mscomm

    I'm trying to convert from Hex to dec in VBA for access 97.

    I am retrieving Hex values from an msComm control which I know are correct from analysing it using some serial monitoring software,

    I want to extract the 2 hex bytes from the below (returned by mscomm control, stored as a 9 byte string), swap them around and convert to a decimal, so:
    ____________\/\/_________
    40 E7 06 49 E6 10 69 00 00

    becomes:10 E6
    which converts to decimal: 4326

    Currently i'm using
    Code:
    Private Sub Convert(hexString)
    Dim intValue As Long
    Dim hexSwap As String
    ' triggered by msComm receive event
    ' hexString from msComm buffer

    hexSwap = (Mid(hexString, 5, 1)) & (Mid(hexString, 4, 1))
    intValue = CLng("&H" & hexSwap)
    MsgBox intValue

    End Sub
    which returns a type mismatch error.

    In summary i wish to:
    take 2 bytes of a received string,
    swap their order,
    concatenate them as 1 hex value,
    convert that value to decimal.

    If anyone can tell me where i'm going wrong i'd be very grateful

    Cheers, Dan

  2. #2
    Join Date
    Jun 2004
    Location
    Florida, US
    Posts
    521
    I assume hexstring is a string variable, if it is not it should be handled completely different:

    Code:
    Private Sub Convert(hexString)
    Dim intValue As Long
    Dim hexSwap As String
    ' triggered by msComm receive event
    ' hexString from msComm buffer
    hexSwap = (Mid(hexString, 16, 2)) & (Mid(hexString, 13, 2))
    intValue = CLng("&H" & hexSwap)
    MsgBox intValue
    End Sub
    as you can see i only changed the parameters of the mid functions.
    ghozy.

  3. #3
    Join Date
    Jun 2004
    Location
    Florida, US
    Posts
    521
    though if it is a byte code inserted in a string, following code will do it:
    Code:
     
    Private Sub Convert(hexString)
    Dim intValue As Long
    ' triggered by msComm receive event
    ' hexString from msComm buffer
    intValue = (AscB(Mid(hexString, 6, 1)) * 256) + AscB(Mid(hexString, 5, 1))
    MsgBox intValue
    End Sub
    a bytecode serie in a string is not really hex. they are bytes and considered like small integers. therefore you don't do string operations on them. instead you convert them to long integers and do what you want.
    ghozy.

Posting Permissions

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