Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private NativeMe As JavaObject
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Dim testStr As String = "9142656"
If FirstTime Then
NativeMe.InitializeContext
End If
' ( https://www.lammertbies.nl/comm/info/crc-calculation.html )
' CRC-16 0x8005 x16 + x15 + x2 + 1
' CRC-CCITT 0x1021 x16 + x12 + x5 + 1
' CRC-DNP 0x3D65 x16 + x13 + x12 + x11 + x10 + x8 + x6 + x5 + x2 + 1
' CRC-32 0x04C11DB7 x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x1 + 1
' Code is Solution is based from the sample found in princeton university http://introcs.cs.princeton.edu/java/61data/CRC16CCITT.java
' https://stackoverflow.com/questions/24694713/calculation-of-ccitt-standard-crc-with-polynomial-x16-x12-x5-1-in-java
Dim Result As String = NativeMe.RunMethod("getCRC16CCITT", Array(testStr, 0x8005, 0xFFFF, False))
Log(Result)
End Sub
#if java
/**
* converts the given String to CRC16
*
* @param inputStr
* - the input string to get the CRC
* @param polynomial
* - the polynomial (divisor)
* @param crc
* - the CRC mask
* @param isHex
* - if true, treat input string as hex, otherwise, treat as
* ASCII
* @return
*/
public String getCRC16CCITT(String inputStr, int polynomial,
int crc, boolean isHex) {
int strLen = inputStr.length();
int[] intArray;
if (isHex) {
if (strLen % 2 != 0) {
inputStr = inputStr.substring(0, strLen - 1) + "0"
+ inputStr.substring(strLen - 1, strLen);
strLen++;
}
intArray = new int[strLen / 2];
int ctr = 0;
for (int n = 0; n < strLen; n += 2) {
intArray[ctr] = Integer.valueOf(inputStr.substring(n, n + 2), 16);
ctr++;
}
} else {
intArray = new int[inputStr.getBytes().length];
int ctr=0;
for(byte b : inputStr.getBytes()){
intArray[ctr] = b;
ctr++;
}
}
// main code for computing the 16-bit CRC-CCITT
for (int b : intArray) {
for (int i = 0; i < 8; i++) {
boolean bit = ((b >> (7 - i) & 1) == 1);
boolean c15 = ((crc >> 15 & 1) == 1);
crc <<= 1;
if (c15 ^ bit)
crc ^= polynomial;
}
}
crc &= 0xFFFF;
return Integer.toHexString(crc).toUpperCase();
}
#end if