B4J Question Unsigned Integers?

techknight

Well-Known Member
Licensed User
I am doing a calculation which includes an entire byte array, simply adding all the bytes together into a 32-bit int.

Trouble is, its treating it as signed. So some additions are actually subtractions.

I dont want this happening, I want to add all absolute values of each byte to come out with an entirely positive 32-bit int. (This is a checksum algo for custom hardware)

any ideas?
 

Daestrum

Well-Known Member
Licensed User
B4X:
 Dim bytes() As Byte = Array As Byte(1,2,253,100,74,129)
 Dim total As Int = 0
 For Each v As Byte In bytes
  total = total + Bit.And(v,0x00ff) ' extend the value to short
 Next
 Log(total)
'  check result 
Log(1+2+253+100+74+129)
 
Upvote 0

techknight

Well-Known Member
Licensed User
Nice! Ill give it a shot.

In case your curious, Here is the matching 68K Code:

B4X:
    ;Test for the correct checksum and boot program. 
    movea.l    ProgramStart,A0            ; Copy Start Address into A0
    move.l    ProgramSize,D1            ; Store Program Size into D1
    clr.l    D2                        ; Clear our operational register 1
    clr.l    D3                        ; Clear our operational register 2
Checksumloop:
    move.b    RTC,D0                    ; Reset Watchdog
    move.b    (A0)+,D2                ; Copy byte from Memory into D2
    add.l    D2,D3                    ; Add contents from memory against register D3
    dbf        D1,Checksumloop            ; If we havent finished, branch until we do

    ;D3 now contains our calculated checksum. Memory address at "Checksum" is the desired result. 
    move.l    Checksum,D2                ; Copy desired checksum into D2 so we can run a comparison.
    cmp.l    D3,D2                    ; Do the checksums match?    
    bne.s    EndHandleExecute        ; If not, the checksum has failed.

Yes, Old world meets new :)
 
Upvote 0
Top