Android Question Problem with XOR and decoding data from Bluetooth.

Piotr85$

Member
Licensed User
Hello
For the start sorry for my bad english.

I try use the xor function with 2 bytes.



B4X:
Example: I use this on othe kompiler Basic.
dim keydec as byte=10
dim byte(20) as byte

For i=0 to something.lenght
byte(i)=byte(i) xor keydec
byte(i)=byte(i) xor (i)
next
' my data is
data1=byte(1)
data2=byte(2)
'da..........to end of all
How I can do this in the B4A. The other quastion is if I read the transmition from MCU I get byte value=0 to 255. I not will be problem for decoding transmition if B4A use the value -127 to 128. I write the program on the MCU and work corectly I see properly value on display, but on the B4A I don't know how do this.
 
Last edited:

Piotr85$

Member
Licensed User
I have device with UART trasmition and before I use the MCU with display or PC to read data. The now it's time use as display the smartphone and read the data. I read properly frame from device(but it's coded) and I want decode. I know all parametrs how decode this frame but I only write program on MCU(Bascom). I do not understand how decode frame in B4A. I have the problem with XOR byte from data.
Look at of my code above.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub SignedToUnsigned(b As Byte) As Int
Return Bit.And(b, 0xFF)
End Sub
 
Upvote 0

Piotr85$

Member
Licensed User
B4X:
Sub SignedToUnsigned(b As Byte) As Int
Return Bit.And(b, 0xFF)
End Sub
It's only look the value as Int but I must XOR the byte. ON C programi I write this and work properly. The same work on Bascom


for ( int i=4; i<data; i++ )
{
mybyte = byte ^ keydec ^ (~(unsigned char)(i));
byte =mybyte;
}

 
Last edited:
Upvote 0

Piotr85$

Member
Licensed User
It's Long to write because the many times use the XOR on the decoding. I give only the fragment of code.
The only interest me. If I calculate supose: Byte1=0x56 and value from loop FOR NEXT I will be have correct Value. Example
dim byte1 as byte=0x56
dim result(255) as byte
dim data as int=60
dim key as byte=0xB2
dim Mytrue_data(255) as byte


For i=0 to data
result(i)= bit.xor(byte1,(i))
result(i)=bit.xor(result(i),key)
mytrue_data(i)=result(i)
next

The calculate value will be the same as calculate valu on MCU ??. I think the function Bit.XOR is not he same work like MCU XOR. Because I rewrite the code and the value never pass to nothing. I think that can be, because in the Tutorial oF B4A the write. bit.xor(value as int,value as int) .In the MCU the XOR bytes. I do not shure but this. Sorry I not use never B4A I only write some code on MCU and I encountered a problem. When I will be have some time I try write the same code and result from MCU and from B4Android.
 
Upvote 0

Piotr85$

Member
Licensed User
OK I write sample:BASCOM CODE:
B4X:
Dim Ldata As Long
Ldata = 10

Dim Rxdana(255) As Byte


Dim Noti As Byte
Dim A As Byte

Dim Decbyte As Byte


   Config Portb = Output


Do
   Decbyte = &HB2
   Rxdana(1) = 255
   Rxdana(2) = 80
   Rxdana(3) = 60


      For A = 1 To Ldata
         Decbyte = Rxdana(a) Xor Decbyte

         Noti = A
         Noti = Noti - 1
         Noti = Not Noti
         Decbyte = Decbyte Xor Noti
         Rxdana(a) = Decbyte

         Print "RX" ; A ; "=" ; Rxdana(a)





     Next

     Loop

     End
REsult
RX1=178

RX2=28

RX3=221




Basic for android with comments code BAscom
B4X:
Sub timer1_TICK
 
    Dim Ldata As Long
    Ldata = 10

    Dim Rxdana(255) As Byte


    Dim Noti As Byte
    Dim A As Byte

    Dim Decbyte As Byte


 


 
        Decbyte = 0xB2
        Rxdana(1) = 255
        Rxdana(2) = 80
        Rxdana(3) = 60


        For A = 1 To Ldata
            Decbyte = Bit.Xor(Rxdana(a),Decbyte)'Rxdana(a) Xor Decbyte

            Noti = A
            Noti = Noti - 1
            Noti = Bit.Not(Noti) 'not noti
            Decbyte = Bit.Xor(Decbyte,Noti)'Decbyte Xor Noti
            Rxdana(a) = Decbyte

        'Print "RX" ; A ; "=" ; Rxdana(a)

edittext1.Text="RX"&A&Rxdana(A)



        Next

 
 
 
 
 
 
 
End Sub
result after compilation:
RX1=-78
RX2=28
RX3=-35


Someone know how do this to decode properly. This is the only fragment of decoding I many times use XOR so you know it's problem If you have not correct value.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Basic for android
The product name is B4A.

Note that it will be easier to help you if you format the code and make it more readable.

Bytes in B4A are signed (-128 to 127). In most cases you can ignore this fact however if you want to display the value then you can convert it to an unsigned value:
B4X:
Sub ToUnsigned(b As Byte) As Int
   Return Bit.And(0xFF, b)
End Sub
 
Upvote 0
Top