Android Question CRC-16 algorhitm checksum

Lello1964

Well-Known Member
Licensed User
Longtime User
I must calculate crc-16 checksum using a forward CRC-16 algorithm with the polynomial (X16 + X15 + X2 + 1) initialised using the seed 0xFFFF.

example :
0x90 0x01 0x11

crc= 0x26 0x03

Can you help me ?

Thanks

Raffaele
 

Lello1964

Well-Known Member
Licensed User
Longtime User
Hello Erel,
thanks for your replay.

I have tryed to calculate with some of them, but havn't the right result

this is manual information for CRC:

CRC
The final 2 bytes are used for a Cyclic Redundancy Check (CRC). This is provided to detect errors during transmission. The CRC is calculated using a forward CRC-16 algorithm with the polynomial (X16 + X15 + X2 + 1). It is calculated on all bytes except STX and initialised using the seed 0xFFFF. The CRC is calculated before byte stuffing.

0x80 0x01 0x11
CRC must be 0x65 0x82

I don't know what is the problem.

can you help me ?
 

Attachments

  • crc.png
    crc.png
    45.8 KB · Views: 871
Last edited:
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
B4X:
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
 
Upvote 0

Lello1964

Well-Known Member
Licensed User
Longtime User
Hello Erel,
thanks for your replay.

I have tryed to calculate with some of them, but havn't the right result

this is manual information for CRC:

CRC
The final 2 bytes are used for a Cyclic Redundancy Check (CRC). This is provided to detect errors during transmission. The CRC is calculated using a forward CRC-16 algorithm with the polynomial (X16 + X15 + X2 + 1). It is calculated on all bytes except STX and initialised using the seed 0xFFFF. The CRC is calculated before byte stuffing.

0x80 0x01 0x11
CRC must be 0x65 0x82

I don't know what is the problem.

can you help me ?
crc.png
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Hi Raffaele.
The function work without problem

Dim test As String = "800111"
Dim Result As String = NativeMe.RunMethod("getCRC16CCITT", Array(test, 0x8005, 0xFFFF, True))

if you set TRUE you can treat input string as hex, otherwise, treat as ASCII

You read this in Function:

/**
* 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
*/

Anyway this is example:

B4X:
Sub Activity_Create(FirstTime As Boolean)
 'Do not forget to load the layout file created with the visual designer. For example:
 
    'This is in HEX value
    Dim testHEX As String =  "800111"

 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(testHEX, 0x8005, 0xFFFF, True))
     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





upload_2019-11-17_13-59-38.png


Idem if you put 0x90 0x01 0x11 ...the return is 326 ( 03 26 )

crc= 0x26 0x03
 
Upvote 0

Similar Threads

Top