```
Sub Class_Globals
Private NativeMe As JavaObject
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
NativeMe = Me
End Sub
#IF JAVA
/*
* The converted algorithm is simple, using a 32 bit accumulator (checksum),
* we add sequential 16 bit words to it, and at the end, fold back all
* the carry bits from the top 16 bits into the lower 16 bits.
*/
/*
* We start with:
* uint16_t Sys_Checksum(uint16_t init, uint8_t* buff, int32_t len)
*/
public int CalculateCRC(int init, String buff, int len)
{
/* uint32_t i = 0; */
int words = len;
int checksum;
/* We need a counter */
int i = 0;
int temp;
/* uint32_t checksum = init; */
checksum = init;
/* variables for debuging */
String s;
BA.Log(buff);
/* while(len >= 2) */
while(len >= 2){
s = buff.substring(i,i+2);
BA.Log(s.toUpperCase());
/* checksum += ((buff[i] << 8) & 0xff00) | buff[i + 1]; */
/* Get MSB value, clear overflow bit, and clear LSB bits */
temp = ((HexToDecimal(buff.substring(i,i+1))<< 8) & HexToDecimal("FF00"));
BA.Log("MSB = " + Integer.toHexString(temp).toUpperCase());
/* Add LSB value */
temp = temp | (HexToDecimal(buff.substring(i+1,i+2)));
BA.Log(s.toUpperCase() + " = " + Integer.toHexString(temp).toUpperCase());
/* Add result to checksum */
checksum = checksum + temp;
BA.Log("Checksum = " + Integer.toHexString(checksum).toUpperCase());
//checksum = checksum + HexToDecimal(s);
// BA.Log(String.valueOf(checksum));
// BA.Log(i + " - " + Integer.toHexString(checksum));
// move to next byte
i += 2;
len -= 2;
}
BA.Log(Integer.toString(i) + " - " + Integer.toString(len));
BA.Log("Checksum 1 ---> " + Integer.toHexString(checksum).toUpperCase());
/* if(len) */
if (len > 0){
/* Get MSB value for last byte, clear overflow bit, and clear LSB bits */
/* checksum += (buff[i] << 8) & 0xff00; */
temp = ((HexToDecimal(buff.substring(i,i+1))<< 8) & HexToDecimal("FF00"));
BA.Log("MSB = " + Integer.toHexString(temp).toUpperCase());
/* Add result to checksum */
checksum = checksum + temp;
BA.Log("Checksum = " + Integer.toHexString(checksum).toUpperCase());
}
/* while(checksum >> 16) {
* This is special and write as 'checksum >>> 16' in Java which generate a error
* So we use OliverA solution
*/
while((checksum >>> 16) > 0) {
/* right shift and truncate to 16 bits */
checksum = (checksum >> 16) + (checksum & HexToDecimal("ffff"));
}
BA.Log("Checksum " + Integer.toHexString(checksum).toUpperCase());
/* return ~checksum;
* This is 'the bitwise complement operator' Bitwise NOT which doesn't exist in Java
* "note that, in all cases, ~x equals (-x)-1"
*/
temp = (0 - checksum) - 1;
BA.Log("Checksum " + Integer.toHexString(temp).toUpperCase());
return temp;
}
public static int HexToDecimal(String hex){
int decimal=Integer.parseInt(hex,16);
//System.out.println(decimal);
return decimal;
}
#End If
Public Sub CalculateCRC(Init As Int, Buffer As String, len As Int)
' NativeMe.InitializeContext
Dim s As String = NativeMe.RunMethod("CalculateCRC", Array(Init, Buffer, len))
Log(s)
End Sub
```