Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Flag for Zambia asked on

How to convert Hex text in Ms Access VBA to Binary code

Dear All;
I need help to convert some Hex text to binary code using Ms Access VBA, see below:
Header1 :  0X1A
Header2 : OX5D
CmdID: (0x01,0x02 & 0x03)
Length : ( Content to big-endian)
I have tried to convert the above string using the online convertor, see the screen shoot below, for example
(1)       0x01 = 00110000 01111000 00110000 00110001
The objective here is send a command string to the serial port in the recommended format below:
<Header1><Header2><CmdID><Length><Content><CRC>
Both the content & CRC is already done , the problem is the four ( Header1 to Length)

Simple VBA code:

Dim Header1 as string, Header2 as string , CmdID as string, length as string, Content as string , CRC as string
Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4
Dim lngStatus As Long
Dim strError  As String
Dim strData   As String

Header1 = XXXX (converted to binary code)
Header2 = XXXX (converted to binary code)
CmdID = XXXX (converted to binary code)
Length = XXXX (converted to binary code)
Content =????? (converted to binary code all in Json)
CRC = XXXX (converted to binary code)
strData = <Header1><Header2><CmdID><Length><Content><CRC>
  ' Initialize Communications
    lngStatus = CommOpen(intPortID, "COM" & CStr(intPortID), _
        "baud=9600 parity=N data=8 stop=1")
    
    If lngStatus <> 0 Then
	' Handle error.
        lngStatus = CommGetError(strError)
	MsgBox "COM Error: " & strError
    End If
    

    ' Set modem control lines.
    lngStatus = CommSetLine(intPortID, LINE_RTS, True)
    lngStatus = CommSetLine(intPortID, LINE_DTR, True)

    ' Write data to serial port.
    lngSize = Len(strData)
    lngStatus = CommWrite(intPortID, strData)
    If lngStatus <> lngSize Then
    ' Handle error.
    End If

Open in new window

That is what is required to communicate to this serial device.



string-to-binary.png
Microsoft AccessVBA

Avatar of undefined
Last Comment
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc

8/22/2022 - Mon
John Tsioumpris

You can check this thread here : http://www.vbaexpress.com/forum/showthread.php?45350-Solved-Hex-to-Binary-conversion-not-using-HEX2BIN
Essentially they are converting hexadecimal to decimal and then decimal to bin
ASKER CERTIFIED SOLUTION
Gustav Brock

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc

ASKER
Thank you so much Gustav Brock!

Just one more issue , I tested the code its okay , I'm just asking whether its possible also to factor the following Byte length:

Header1 = 1 Length(Byte)
Header2 = 1  Length(Byte)
CmdID = 1  Length(Byte)

How to I get values at one go instead of calculating one by one , see the VBA code  below ( Example Header1,Header2 & CmdID):

Private Sub CmdWhext_Click()
DecimalValue = &H1A
BinaryValue = DecToBin(DecimalValue, 8)
Debug.Print DecToBin(DecimalValue, 8)
End Sub


Option Compare Database

Option Explicit

Public Function DecToBin(ByVal lngNumber As Long, Optional bytLength As Byte) As String

' Returns string that represents the binary expression for lngNumber.
'
' If bytLength is specified, returned string will be filled with
' leading zeroes up to this length.

  Dim strBin As String
 
  While lngNumber > 0
    strBin = (lngNumber Mod 2) & strBin
    lngNumber = lngNumber \ 2
  Wend
  If bytLength > 0 Then
    strBin = Right(String(bytLength, "0") & strBin, bytLength)
  End If
 
  DecToBin = strBin

End Function
Gustav Brock

I would convert each part to the desired length, then simply concatenate the converted parts - to have the complete and full text string of bits.

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc

ASKER
Okay thank so much for the help.

Regards

Chris.