B4J Question Misbehaving Byte array

barx

Well-Known Member
Licensed User
Longtime User
I guess i'm missing something here. If I have the following code:

B4X:
Dim serial() As Byte = Array As Byte(0x53, 0x32, 0x33, 0x31, 0x33, 0x31, 0x57, 0x34, 0x33, 0x39)
Dim LoginString() As Byte = Array As Byte(0x55, 0x79, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0b, serial(0), serial(1), serial(2), serial(3), serial(4), serial(5), serial(6), serial(7), serial(8), serial(9), 0x01)
Dim Chksum() As Byte = CalcChecksum(LoginString)

And then Break at the CalcChecksum method and poke into the passed data
2017-04-02 (1).png

As you can see, the first 2 bytes are fine but the 3rd is wrong. It is 0xFFFFFF80 instead of 0x80.

I've been tinkering about and basically anything > 0x79 shows wrong. Anthing < 0x79 is fine.

Any ideas?
 

Daestrum

Expert
Licensed User
Longtime User
Looks like the debugger just sign extends it to 32bit to display it.
All bytes in java are signed and can have a value of -128 to 127, not like 'c' where an unsigned byte can have a value of 0 to 255.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Looks like the debugger just sign extends it to 32bit to display it.
All bytes in java are signed and can have a value of -128 to 127, not like 'c' where an unsigned byte can have a value of 0 to 255.


So how do I make a byte of say 0xaa. I have tried just using 170 (the decimal of 0xaa) but still the same. I don't believe it is just the ide showing it wrongly as the resultant checksum that is returned is calculated wrongly.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Putting them in the byte buffer use 0xaa
If you want to see the value as 170 then you will need to put the byte value into a short using something like
B4X:
Dim s As Short
s = Bit.And(yourValue,0xff)
log(s)
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Can't see your checksum calculation but it's probably similar to this
Note one way (s) will calculate correctly, (s1) will be incorrect.
B4X:
Dim s As Short = 0
Dim s1 As Short = 0
For Each d As Byte In c ' c is the byte buffer
s = s + Bit.And(d,0xff) ' makes value 0 to 255
s1 = s1 + d ' wrong - not allowing for -ve values
Next
' this is correct as it only adds +ve numbers
Log("s "& s)
Log(Bit.UnsignedShiftRight(Bit.And(s,0xff00),8)&" "&Bit.And(s,0xff))
' this is wrong value as it calculated on the value not the 0 - 255 value
Log("s1 "& s1) ' wrong value as not allowing for -ve values
Log(Bit.UnsignedShiftRight(Bit.And(s1,0xff00),8)&" "&Bit.And(s1,0xff))
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Can't see your checksum calculation but it's probably similar to this
Note one way (s) will calculate correctly, (s1) will be incorrect.
B4X:
Dim s As Short = 0
Dim s1 As Short = 0
For Each d As Byte In c ' c is the byte buffer
s = s + Bit.And(d,0xff) ' makes value 0 to 255
s1 = s1 + d ' wrong - not allowing for -ve values
Next
' this is correct as it only adds +ve numbers
Log("s "& s)
Log(Bit.UnsignedShiftRight(Bit.And(s,0xff00),8)&" "&Bit.And(s,0xff))
' this is wrong value as it calculated on the value not the 0 - 255 value
Log("s1 "& s1) ' wrong value as not allowing for -ve values
Log(Bit.UnsignedShiftRight(Bit.And(s1,0xff00),8)&" "&Bit.And(s1,0xff))

Thanks for that @Daestrum

This is current sub. I had found that adding 0x100 to negative values, seemed to sort the issue, though I have not fully tested it nor thought about the theory too much to know if it theoretically should work.

B4X:
Sub CalcChecksum(Data() As Byte) As Byte()
   
    Dim Total As Int
    Dim Byte1, Byte2 As Byte = 0x00
   
    For i = 0 To Data.Length - 1
        If Data(i) < 0 Then
            Total = Total + Data(i) + 0x100
        Else
            Total = Total + Data(i)
        End If
       
    Next
   
    Byte2 = Bit.And(Total, 0xff)
    Byte1 = Bit.ShiftRight(Total, 8)
   
    Return Array As Byte(Byte1, Byte2)
   
End Sub

I will try out your solution as it looks a lot neater ;)
 
Upvote 0
Top