|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
checksum theory?
Hi everyone,
I was trying to put some error checking in this file serialization I was doing. I was wondering about how checksum works - is it basically like you add up the values of all the variables you're serializing then store that in one variable and also write that to the same file - then when you read it back in, if all the read in varaibles' values don't equal the checksum value from the last save, then you know the file is corrupt / messed up? Code:
WriteFile()
{
int x = 5;
int y = 10;
CString z = "hello";
int nCheckSum = (x + y + ASCII value of 'hello'?);
WriteTheFile();
}
ReadFile()
{
int x, y, z, nCheckSum;
CString z;
ar >> x;
ar >> y;
ar >> z;
ar >> nCheckSum;
if (x + y + ASCII value of z? != nCheckSum)
return false;
}
Thanks! |
|
#2
|
||||
|
||||
|
Yeah that's about it. There's lots of different checksum algorithms. CRC16 and CRC32 are fairly common: http://www.vbaccelerator.com/home/V...C32/article.asp .
If simplicity is all you care about, you could just add up the ASCII values of all the characters. This is generally not very reliable though, because swapping two characters will yield the same checksum, and so will say, changing a 'b' to a 'c' and a 'x' to a 'w' in the same file. The MD5 hashing algorithm is very reliable, and you can find implementations of it all over the place; but it's probably a bit of overkill. That's the general idea though. Compute checksum, store somewhere. Then, when you read the data back in, compute checksum, compare it with stored checksum. Checksums are useful for detecting data corruption because the chance of both the data being corrupted AND the checksum being accidently corrupted to the "correct" value are very slim indeed. |
|
#3
|
|||
|
|||
|
Ok thanks alot for the info - I was thinking about what you said about someone being able to switch like two chars around in a string and the sum will still be the same - I was gonna try a + - operation on every other char value when checksumming that type, so that if the order of chars is switched around it should produce a different sum - I think subtraction and addition wasn't commutative or whatever that word was from middle school
5 + 4 - 3 + 2 = 8 5 + 3 - 4 + 2 = 6 Thanks! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > checksum theory? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|