C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 26th, 2003, 10:19 PM
theRaginCajun theRaginCajun is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: USA
Posts: 144 theRaginCajun User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
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!

Reply With Quote
  #2  
Old November 26th, 2003, 11:14 PM
peenie's Avatar
peenie peenie is offline
Google Relay Server
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2003
Location: Oh christ I don't even know any more.
Posts: 1,809 peenie User rank is Captain (20000 - 30000 Reputation Level)peenie User rank is Captain (20000 - 30000 Reputation Level)peenie User rank is Captain (20000 - 30000 Reputation Level)peenie User rank is Captain (20000 - 30000 Reputation Level)peenie User rank is Captain (20000 - 30000 Reputation Level)peenie User rank is Captain (20000 - 30000 Reputation Level)peenie User rank is Captain (20000 - 30000 Reputation Level)peenie User rank is Captain (20000 - 30000 Reputation Level)peenie User rank is Captain (20000 - 30000 Reputation Level)  Folding Points: 9953 Folding Title: Novice Folder
Time spent in forums: 2 Weeks 1 Day 18 h 24 m 10 sec
Reputation Power: 210
Send a message via AIM to peenie Send a message via MSN to peenie
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.

Reply With Quote
  #3  
Old November 27th, 2003, 02:36 AM
theRaginCajun theRaginCajun is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: USA
Posts: 144 theRaginCajun User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > checksum theory?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 10 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek