Discuss checksum theory? in the C Programming forum on Dev Shed. checksum theory? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.
Posts: 144
Time spent in forums: < 1 sec
Reputation Power: 9
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;
}
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.
Posts: 144
Time spent in forums: < 1 sec
Reputation Power: 9
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