I have a client that gave me the following test datastring:
AAA|E0071001|D|20090401010101|EC|UKDC|BP|********|1|TR01|
CPD|AAA123|Test Participant A123|P|BBB456|New Participant B456|P|
ER2|NAHNAH1|Test Participant|20090401||
EAD|7|||
ZZZ|5|474617386|
The first row is considered header data (starts with AAA), the last row is considered a footer (ZZZ). The rest is the body (lines starting with : CPD,ER2,EAD)
The footer contains a checkSum value (here : 474617386). I need to be able to arrive with this value myself following these rules :
- Init result variable to 0
- consider each record in turn (including header, but excluding footer)
- Break each record into four byte (character) sections (excluding he end of the line character), padded with nulls if required, and exclusive OR (XOR) them into the result variable.
The client provides a c like pseudo code :
num_chars = strlen (record_buffer)
FOR (i = 0;i < num_chars;)
value = 0
FOR (j = 0; j < 4; i++, j++)
IF i < num_chars
value = ((value << 8 ) + record_buffer[i])
ELSE
value = value << 8
END IF
ENDFOR
result = result XOR value
ENDFOR
I thought I could translate this straight to C# doing the following :
var result = 0;
var num_chars = record_buffer.Length;
for (int i = 0; i < num_chars;)
{
var value = 0;
for (int j = 0; j < 4;i++, j++)
{
if (i < num_chars)
value = ((value << 8) + record_buffer[i]);
else
value = value << 8;
}
result = result ^ value;
}
Unfortunately my results are always off. Reading in the entire string or reading each record (row) and then summing them up.
I would be thankful for your help.