Need to calsulate the Checksum of a string

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Here is the requirement:
<span style="font-size:small
The Checksum is initially set to zero and is the 16-bit binary addition (excluding parity if applicable) of all characters after, but not including, the SOH character and through (and including) the ETX character. The Checksum is then represented as four
ASCII-Hex characters for transmission.
<span style="font-size:small I searched around and found the following code:
<span style="font-size:small <span style="font-size:small <span style="font-size:small
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; private <span style="color:Blue; static <span style="color:Blue; string GetChecksum(<span style="color:Blue; string s)
{
<span style="color:Blue; int checksum = 0;
<span style="color:Blue; foreach (<span style="color:Blue; char c <span style="color:Blue; in s)
{
checksum += Convert.ToByte(c);
}
<span style="color:Blue; return checksum.ToString(<span style="color:#A31515; "X4");
}
[/code]

<span style="font-size:small <span style="font-size:small <span style="font-size:small But I have two problems:
<span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small 1. I dont know what its doing.
<span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small 2. It doesnt match what I have for a good packet that works.

<span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small If I substitue a good string for a packet that works, and run it through the
code above, I get a different value than im expecting. Unfortunately, do to the nature of the industry, I wont be able to include the actuall data in the string. But I can say that it is variable length and includes both alpha and numeric charactors.
<span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small Any
help is very much apprciated.
<span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small Thanks Jim


View the full article
 
Back
Top