![]() |
| | #1 |
| Registered User Join Date: Jan 2009
Posts: 151
| Making of checksum using XOR Code: typedef struct {
char a;
char b;
char c[10];
} DataPacket;
Code: char checksum;
checksum = a ^ b;
for(i = 0; i < sizeof(c); i++){
checksum ^= c[i];
}
|
| Subsonics is offline | |
| | #2 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Yes, you can. And it will be a slightly "better" checksum than using addition, because all bits have an equal chance of being detected as erroneous. But you can only detect an odd-number of failures for each bit. An addition checksum fails to detect a double failure of the upper-most bit [assuming the checksum is equal size to the transmission unit, e.g. 1 byte checksum]. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #3 |
| Registered User Join Date: Jan 2009
Posts: 151
| Perfect, Thank you. |
| Subsonics is offline | |
| | #4 |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,476
| That's called an LRC. In practice, both checksum and LRC are inferrior to a good CRC.
__________________ My homepage Advice: Take only as directed - If symptoms persist, please see your debugger |
| iMalc is offline | |
| | #5 |
| Registered User Join Date: Jan 2009
Posts: 151
| Aah, I see. I´m trying to comunicate with a device that uses checksum so I´m stuck with that. The docs I have says "checksum (exclusive OR of above 124 bytes)" I just wanted to make sure that my approach would work or I would be stuck re-sending the first packet. :-) |
| Subsonics is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| checksum sender vs reciever? | silhoutte75 | C Programming | 1 | 04-13-2008 01:27 PM |
| weird checksum function | sagitt13 | C Programming | 7 | 10-31-2006 01:25 AM |
| Help figuring out a checksum algorithm | barem23 | A Brief History of Cprogramming.com | 5 | 01-11-2003 04:24 AM |
| Reading Binary file to find Checksum | Abbila | C++ Programming | 0 | 09-25-2002 09:52 PM |