Thread: Need HELP to calculate Checksum/CRC for a structure! [Urgent Please]

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    3

    Question Need HELP to calculate Checksum/CRC for a structure! [Urgent Please]

    Hi Mates,

    Code:
    struct R_Data
    {
      unsigned short id;
      unsigned char flag;
      float value;
      time_t time_stamp;
    }
    
    struct R_Packet
    {
      struct R_Data packet[100];  // 100 R_Data packets
      unsigned int header; // Sequence number of packet (i.e; For 1st R_Packet variable, header=1,  For 2nd R_Packet variable, header=2 ...)
      unsigned int footer; // will be reserved for checksum/CRC verification at client side
    };
    My task is Sending 600 R_Data structures to the client by packing 100 R_Data structure variables into 1 packet (Structure R_Packet). So, Total 6 R_Packet will be sent from server to client via TCP Stream socket. As i said sequence number of each R_Packet will be stored in its header variable and Footer variable is reserved/used for storing the Checksum.

    I need your help for the implemntation/calculation of checksum or CRC for each R_Packet before sending and the checksum/CRC result will be stored in R_Packet.footer which will be used by client to verify the packet loss.


    My Question is:
    How to calculate the Checksum on this heterogenous type packet?
    Any standard algorithms are there?
    If u have any readymade checksum Code, Please Share!

    I desperately Need your help!


    Thanks and Regards,
    Sravan Kumar.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Any standard algorithms are there?
    Yes, you use google like everyone else would.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    3
    Quote Originally Posted by Salem View Post
    > Any standard algorithms are there?
    Yes, you use google like everyone else would.
    the algorithms which are given are for a single variable or array.
    I don't know how it is applicable for data structure which is having array of 100 structure variables?

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It seems like you would need to know how the client calculates the CRC, otherwise how can you be sure that you use the same method? Aren't you using some kind of protocol, that is documented somewhere, including the checksum method?

    Cyclic redundancy check - Wikipedia, the free encyclopedia
    Last edited by Subsonics; 06-21-2012 at 03:11 AM.

  5. #5
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Sounds like they just need to arrange their packing and then calculate a CRC on the packed data. No need for anything fancy, by the looks of it. Which makes me wonder why a simple Google isn't sufficient.

    Pack your data into your single packet. Then CRC that data. Then change the packet's CRC to the value calculated. Then send the packet.
    Receive a packet. Calculate a CRC of the packed data in that packet. Check it matches the CRC value stored in the packet. Unpack the data.

    At what point do you need to know ANYTHING about the structure of the data to calculate the CRC? And you only need to know about the structure of the data AT ALL when you first pack that data into the packet, without which you won't be ABLE to pack that data into the packet in the first place.

    Quite what's so difficult about this, I don't know. If you've packed the data into a stored array in the packet, just calculate the checksum of that area of memory that holds the stored array (and nothing more). They are two, separate, distinct steps that have nothing to do with each other.

    Also, the barest minimum CRC / hashing example will provide you with EVERY bit of code you need in terms of the CRC / hash here. And every CRC / hash library I've ever seen is at most two functions - initialise hash, add data to hash. The return value of the latter is almost always the CRC/hash of the data so far.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by ledow View Post
    Sounds like they just need to arrange their packing and then calculate a CRC on the packed data. No need for anything fancy, by the looks of it.
    Yes but how, there are more ways than one to calculate a checksum. If the sending part isn't calculating the checksum the same as the receiving end, then the receiver would think that the data is corrupted because the checksum doesn't match.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    3
    Quote Originally Posted by ledow View Post
    Sounds like they just need to arrange their packing and then calculate a CRC on the packed data. No need for anything fancy, by the looks of it. Which makes me wonder why a simple Google isn't sufficient.

    Pack your data into your single packet. Then CRC that data. Then change the packet's CRC to the value calculated. Then send the packet.
    Receive a packet. Calculate a CRC of the packed data in that packet. Check it matches the CRC value stored in the packet. Unpack the data.

    At what point do you need to know ANYTHING about the structure of the data to calculate the CRC? And you only need to know about the structure of the data AT ALL when you first pack that data into the packet, without which you won't be ABLE to pack that data into the packet in the first place.

    Quite what's so difficult about this, I don't know. If you've packed the data into a stored array in the packet, just calculate the checksum of that area of memory that holds the stored array (and nothing more). They are two, separate, distinct steps that have nothing to do with each other.

    Also, the barest minimum CRC / hashing example will provide you with EVERY bit of code you need in terms of the CRC / hash here. And every CRC / hash library I've ever seen is at most two functions - initialise hash, add data to hash. The return value of the latter is almost always the CRC/hash of the data so far.

    Thanks for the suggestion ledow.
    Yeah Google will gives tons of theory!
    I am very new to this CRC stuff! So, i am unable to get the full context of this algorithm application.
    Do you have any source code snippet for CRC calculation of a single packet? and How to change the packet's CRC to the value calculated?
    Please, share me sir!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Oh please, just try appending "source code" to whatever search terms you're using.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Take the address of your structure, cast that to a pointer to char or whatever the algorithm you find takes, pass in that pointer and the length, and you're done.

    Seriously, it is that easy!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Quote Originally Posted by shravansofts View Post
    Yeah Google will gives tons of theory!
    I am very new to this CRC stuff! So, i am unable to get the full context of this algorithm application.
    Do you have any source code snippet for CRC calculation of a single packet? and How to change the packet's CRC to the value calculated?
    I suggest you listen to your teacher on whatever course it is you're doing, or do some simple research. Calculating a CRC is something that there's several thousand pieces of example code out there for, whichever one you happen to need (and if you are able to just choose "any" algorithm, choose one of the standardised CRC ones - the Wikipedia article on CRC will show you some). But, honestly, I can't believe that you want to write this code "urgently" and can't get your head around the results of a simple Google search, bother to look on something like Wikipedia (which will come up in the top ten Google searches for it, I should think) or know how to put the CRC into the packet (you do know what a CRC is, right? And how it would be represented in memory? And the whole POINT of the structures that you've been given with their members explicitly reserved for CRC storage?).

    We help those who help themselves here. Just what, precisely, have you done so far apart from re-stating the task you were given? All I see is a homework question. We don't answer those, we only steer. We've steered you plenty now. What have you actually got out of it? What progress have you made? How much data-packing, CRC-calculating code have you actually got written now? Seriously, this is first-year programming stuff and you can't even find your own source on Google? You wouldn't have lasted two seconds programming in the pre-Google days where you had to read up on this stuff in books, or arrive at a solution of your own, and then write the code yourself.

    And, to answer part of your reply, programming is 50% theory, 10% writing, 40% debugging. If you can't read the "theory" out there on these things and at least knock up a bit of consistently-working (if not 100% accurate) code, then I suggest you find yourself another task, course, hobby or career.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating CheckSum
    By drkidd22 in forum C++ Programming
    Replies: 10
    Last Post: 01-17-2011, 08:28 PM
  2. crc32/checksum app
    By andwan0 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2010, 07:45 PM
  3. CheckSum help
    By remoir in forum C Programming
    Replies: 13
    Last Post: 02-01-2009, 10:19 PM
  4. CRC / checksum
    By Zoltarc in forum C++ Programming
    Replies: 0
    Last Post: 11-30-2002, 11:05 PM
  5. Old PC Checksum Error...
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 12-04-2001, 02:06 PM

Tags for this Thread