I used the C language for 22 minutes and then abandoned it.
But I think it must be translated this way. The most expert will be able to correct.
B4X:
Sub Calc_CRC (Data () As Byte) As Int
'ushort r=0, a, b;
'short t;
Dim r = 0, a, b As Int
For t=0 To Data.Length-1
'r+=(t*Data[t])+t;
r = r + (t * Data(t))+t
Next
'a=r>>8; if (a==0x0D) a=0x0E;
a=Bit.ShiftRight(r,8): If (a=0x0D) Then a=0x0e
'b=r&0xFF; if (b==0x0D) b=0x0E;
b=Bit.And(r,0xFF): If (b=0x0d) Then b=0x0e
'a<<=8;
a=Bit.ShiftLeft(a,8)
'return a|b;
Return Bit.Or(a,b)
End Sub
Of course, but I do not think we can do this in B4X and maybe it's not necessary based on the algorithm.
If we understand what we need, we might understand better.
If I try to translate the algorithm and not the code ... it could be translated as well:
B4X:
Sub calculate_crc(Data () As Byte) As Long
'ushort r=0, a, b;
'short t;
Dim r As Long = 0
Dim a, b As Int
For t=0 To Data.Length-1
'r+=(t*Data[t])+t;
r = r + (t * Data(t))+t
Next
'a=r>>8; if (a==0x0D) a=0x0E;
a=r/256: If (a=13) Then a=14
'b=r&0xFF; if (b==0x0D) b=0x0E;
b=r Mod 256: If (b=13) Then b=14
'a<<=8;
'return a|b;
Return ((a * 256)+b)
End Sub
i mean this part, i guess it give different result. btw i saw that b4a understand UByte but i do not know which lib it is.
r = r + (t * 0)+t
r = r + (t * -128)+t
This is easy to solve, just state Char. and then translate into value with Ascii ... see example
B4X:
Sub calculate_crc(Data () As Char) As Long
'ushort r=0, a, b;
'short t;
Dim r As Long = 0
Dim a, b As Int
For t=0 To Data.Length-1
'r+=(t*Data[t])+t;
r = r + (t * Asc(Data(t)))+t
Next
'a=r>>8; if (a==0x0D) a=0x0E;
a=r/256: If (a=13) Then a=14
'b=r&0xFF; if (b==0x0D) b=0x0E;
b=r Mod 256: If (b=13) Then b=14
'a<<=8;
'return a|b;
Return ((a * 256)+b)
End Sub
also can write:
B4X:
Sub calculate_crc(Data () As Byte) As Long
'ushort r=0, a, b;
'short t;
Dim r As Long = 0
Dim Ap as long
Dim a, b As Int
For t=0 To Data.Length-1
'r+=(t*Data[t])+t;
AP = Data(t): If Ap <0 then Ap=Ap+256
r = r + (t * Ap)+t
Next
'a=r>>8; if (a==0x0D) a=0x0E;
a=r/256: If (a=13) Then a=14
'b=r&0xFF; if (b==0x0D) b=0x0E;
b=r Mod 256: If (b=13) Then b=14
'a<<=8;
'return a|b;
Return ((a * 256)+b)
End Sub
For every problem there is a solution. Do not despair
I do not know. The translation seems correct, as I told you, not development in C.
Erel who also knows the C language has suggested only a change to the code.
Which of the 3 did you use?
The byte Array from where you get it. Are you sure it's not a string?
Maybe if you tell us this CRC what it refers to (containers, packages, shipping, tax codes) maybe we can find the original algorithm on the web.