Create Block Check Character

Bernardrose

Member
Licensed User
Longtime User
Below is a piece of Basic code to create a block check character used in a serial comms program I use. I am slowly converting the program into B4a.
Could someone give me a helpig hand.
A set of characters are inputted to send to a serial device. A block check character is added to the end of message. The other device uses the same logic to check the message.
REM Create Block Check Character
I%=0
BC%=0
REPEAT
I%=I%+1
A%=ASC(MID$(CMD$,I%,1))
X%=BC% AND A%
X%= NOT X%
BC%=BC% OR A%
BC%=BC% AND X%
UNTIL I%=LEN(CMD$)

Thanks in anticipation,
Bernard
 

Bernardrose

Member
Licensed User
Longtime User
Can you give an example of a string with the block character?

Here are some examples. The last character is the block check character.
aF02317\
aR3
aC02014RG
The string can be two characters upto eight.
The block check character can be any valid Ascii symbol.
 
Upvote 0

MLDev

Active Member
Licensed User
Longtime User
B4X:
' Create Block Check Character
Dim CMD As String = "aC02014R"
Dim I As Int = 0
Dim BC As Int = 0
Dim A, X As Int

Do Until I = CMD.Length
   A = Asc(CMD.CharAt(I))
   X = Bit.And(BC, A)
   X = Bit.Not(X)
   BC = Bit.Or(BC, A)
   BC = Bit.And(BC, X)
   I = I + 1
Loop
   
Log(Chr(BC))

This returns "G"
 
Upvote 0

Bernardrose

Member
Licensed User
Longtime User
MLDev,
Thanks MLDev,the code worked fine. I wonder if you could help me on my next problem. Using the same example, the leading character(a) would be added by the code after C02014R had been inputted from the screen. Then after the block check character was worked out, (Chr(BC)) would be added to the end of the string. This is an easy task in Basic, but I am struggling to make it work in this enviroment.
Best regards,
Bernard
 
Upvote 0

MLDev

Active Member
Licensed User
Longtime User
You use the "&" character for concatenation.

B4X:
someString = "C02014R"
someString = "a" & someString & Chr(BC)
 
Upvote 0
Top