We help IT Professionals succeed at work.

How to convert the ? into length(bytes) in VBA Ms Access

Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc asked
on
I'm almost done with serial port data format parameters in Ms Access, now I need help on the last part of parameter listed below where the length byte is given ? how to treat it in binary conversion:

Fied                                                                     Length(Byte)

Content                                                                                        ?

so what do I put in the converter below under Length ????????? ( Could it be 0 or does ? represent somthing?):

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 

Open in new window

Comment
Watch Question

Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:

Are you saying you have cases where when you call this DecToBin() function it returns a question mark?


Are are you asking some other question about what value to place into the data packet that you are building to send out the serial port, and you are not sure what value it should be?  If this is the case I'd need to see the specification for the data packet you are building...

ste5anSenior Developer

Commented:
Computer basics:

Data is represented by bits. Depending on the used platform and software certain numbers of bits are grouped together. In the Intel-world this is typical

4 bits = 1nibble
2 nibble = 1 byte
2 byte = 1 word
2 word = 1 dword
4 word = 1 qword

As Long is a dword its binary representation has 32 bits. But your functions stops when it reaches the most significant set bit to one, cause leading zeros have no meaning in math. Thus the optional parameter is for controlling the necessary padding.

Instead of an optional parameter the first parameter should be variant and the padding should be used according to the given data type of the number.
bytLength is of byte datatype, it can hold values from 0 to 254.

if your lngNumber is 3  ( binary: 11), and your  bytLength  is 16 then according to the comments in your code you will have to fill the trailing spaces with zeroes

0000000000000011
(16 length)
 
all the above is asuming I understood your question correctly.
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:

As the example values in your previous question were:

Header1 : 0X1A
Header2 : 0X5D

they seem to by two bytes, thus 8 should be used as the length to obtain output like:

 "00011010"