hex2dec conversion using mscomm

G

Guest

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

Marshall Barton

DannyT said:
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 red 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

There seems to be some misperceptions here. If that is a 9
byte string, then it does not contain Hex values as string
characters. As all values in a computer are in binary, you
are thinking Hex only because that is the format that is
presented to you. If this is the case, then you can convert
to a long integer using this expression:

intValue=Asc(Mid(hexString,5,1)+256*Asc(Mid(hexString,6,1)


If that is truely a string of characters, then there are 18
(or 26 if the spaces are included) characters and the
expression would be:

hexSwap=(Mid(hexString,11,2)) & (Mid(hexString,9,2)
intValue = CLng("&H" & hexSwap)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Help Using Excel 2003 to send emails 5
RC4 StrToHex function 1
RGB 4
Big-Endian bit conversion 0
Perl Conversion 1
Conversion from Number to Word!! Aki 1
Re Code Help Please 1
Code help Please 2

Top