Calculate checksum for Laboratory Information System (LIS) frames

Finally I got answer, here is the code for calculating checksum:

private string CalculateChecksum(string dataToCalculate)
{
    byte[] byteToCalculate = Encoding.ASCII.GetBytes(dataToCalculate);
    int checksum = 0;
    foreach (byte chData in byteToCalculate)
    {
        checksum += chData;
    }
    checksum &= 0xff;
    return checksum.ToString("X2");
}

You can do this in one line:

return Encoding.ASCII.GetBytes(dataToCalculate).Aggregate((r, n) => r += n).ToString("X2");

Tags:

C#

Checksum