Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8581733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:07:25+00:00 2026-06-11T21:07:25+00:00

I have an Avl Packet that I’m recieving through GPRS from a Device. The

  • 0

I have an Avl Packet that I’m recieving through GPRS from a Device.

The protocol manual says the packet has a 16bit CRC on the last 4 bytes and a source code for CRC calculation is given:

public static int getCrc16(Byte[] buffer) {
    return getCrc16(buffer, 0, buffer.length, 0xA001, 0);
    }

public synchronized static int getCrc16(Byte[] buffer, int offset, int bufLen, int polynom, int preset) {
    preset &= 0xFFFF;
    polynom &= 0xFFFF;
    int crc = preset;
    for (int i = 0; i < bufLen; i++) {
        int data = buffer[i + offset] & 0xFF;
        crc ^= data;
        for (int j = 0; j < 8; j++) {
            if ((crc & 0x0001) != 0) {
                crc = (crc >> 1) ^ polynom;
            } else {
                crc = crc >> 1;
            }
        }
    }
    return crc & 0xFFFF;
}

So I get the CRC number the packet sends me,then I call getCrc16 for the Byte array in which I have stored the packet and then compare the two numbers right??

Here is the code I use inside my program:

public static String toBinaryString(byte n) {
    StringBuilder sb = new StringBuilder("00000000");
    for (int bit = 0; bit < 8; bit++) {
        if (((n >> bit) & 1) > 0) {
            sb.setCharAt(7 - bit, '1');
        }
    }
    return sb.toString();
}

    int CalculatedCRC = getCrc16(AvlPacket);
    System.out.println("Calculated CRC= " + CalculatedCRC);

    int index = (AvlPacket.length)-4;
    String BinaryRecievedCRC = "";

    for (int j = 0; j < 4; j++) {
        BinaryRecievedCRC+= toBinaryString(AvlPacket[index]);
        index+=1;
    }

    int RecievedCRC = Integer.parseInt(BinaryRecievedCRC, 2);
    System.out.println("Recieved CRC= " + RecievedCRC);

toBinaryString() converts a byte to it’s binary from and puts it into a string!

So I calculate the CRC through getCrc16() given to me from the manual.Then take an index 4 bytes before the end of the packet so I can read the last 4 bytes and get the CRC sent with the packet!

The for loop takes each of the last bytes and with toBinaryString() combines all them in binary form and into a String!So I got something like 0000000000000000101011011101001 (The manual states that first two bytes are always zeroes because its a 16bit CRC)

So I just parse the Binary String into a signed int and Compare the two CRCs…!

Yet I get Results like :

Calculated CRC= 21395
—–Recieved CRC= 30416

or

Calculated CRC= 56084
—–Recieved CRC= 10504

I’ve tested with many packets and they can’t all have loss of data..And I’m parsing the data too so I know that the data I get is correct!

What am I missing in all this??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:07:26+00:00Added an answer on June 11, 2026 at 9:07 pm

    Problem Solved!

    The problem was that the Packet had 8 other bytes before getting into the Data part!
    So I had to exclude those first 8 bytes along with the last 4 bytes of the sent CRC before calculating the CRC!

    Now the numbers agree and the above code is correct with the exception that the for loop in the getCrc16 function starts from i=8 (so as to skip the first 8 bytes of the packet which do no belong to the Data part!)

    Thank you all for your time!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume that I have two AVL trees and that each element from the first
I have a large AVL Tree which I build some times during program from
Assume that I have two AVL trees and that I know their respective sizes.
I have a data structures homework, that in addition to the regular AVL tree
I have a function that's traversing an AVL tree in level-order. The output format
I have a AVL tree data structure, where every node has its own lock.
have a nice day. I got problem when trying to create an image from
Ok we have a simple udf that takes a XML integer list and returns
Have converted devise new session from erb to Haml but doens't work, this is
I'm working on an AVL tree assignment and I have a quick question about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.