Library tutorials & articles
A Checksum Algorithm
By Joseph M. Newcomer, published on 13 Oct 2001
Page 4 of 4
- Introduction
- Checksums
- Checksum.h
- Checksum.cpp
Checksum.cpp
#include "stdafx.h"
#include "Checksum.h"
/****************************************************************************
* checksum::add
* Inputs:
* DWORD d: word to add
* Result: void
*
* Effect:
* Adds the bytes of the DWORD to the checksum
****************************************************************************/
void checksum::add(DWORD value)
{
union { DWORD value; BYTE bytes[4]; } data;
data.value = value;
for(UINT i = 0; i < sizeof(data.bytes); i++)
add(data.bytes[i]);
} // checksum::add(DWORD)
/****************************************************************************
* checksum::add
* Inputs:
* WORD value:
* Result: void
*
* Effect:
* Adds the bytes of the WORD value to the checksum
****************************************************************************/
void checksum::add(WORD value)
{
union { DWORD value; BYTE bytes[2]; } data;
data.value = value;
for(UINT i = 0; i < sizeof(data.bytes); i++)
add(data.bytes[i]);
} // checksum::add(WORD)
/****************************************************************************
* checksum::add
* Inputs:
* BYTE value:
* Result: void
*
* Effect:
* Adds the byte to the checksum
****************************************************************************/
void checksum::add(BYTE value)
{
BYTE cipher = (value ^ (r >> 8));
r = (cipher + r) * c1 + c2;
sum += cipher;
} // checksum::add(BYTE)
/****************************************************************************
* checksum::add
* Inputs:
* const CString & s: String to add
* Result: void
*
* Effect:
* Adds each character of the string to the checksum
****************************************************************************/
void checksum::add(const CString & s)
{
for(int i = 0; i < s.GetLength(); i++)
add((BYTE)s.GetAt(i));
} // checksum::add(CString)
/****************************************************************************
* checksum::add
* Inputs:
* LPBYTE b: pointer to byte array
* UINT length: count
* Result: void
*
* Effect:
* Adds the bytes to the checksum
****************************************************************************/
void checksum::add(LPBYTE b, UINT length)
{
for(UINT i = 0; i < length; i++)
add(b[i]);
} // checksum::add(LPBYTE, UINT)
Related articles
Related discussion
-
VS2005 app's won't run on another machine
by ted4444 (0 replies)
-
VB.NET: Hide and show table using radio buttons
by converter2009 (1 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
How to create a games like FIFA08
by mawcot (0 replies)
-
Binary Studio | software development outsourcing Ukraine
by shane124 (4 replies)
This thread is for discussions of A Checksum Algorithm.