0

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 :

  1. Init result variable to 0
  2. consider each record in turn (including header, but excluding footer)
  3. 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.

Aiolos
  • 28
  • 6
  • Have you tried doing it by hand with a calculator and verifying the results? – bruceg Apr 27 '17 at 05:16
  • what is the record_buffer you are using? – DesertFox Apr 27 '17 at 05:19
  • @DesertFox - I have tried the following things: 1.single string consisting of the first 4 rows above (excluding end of line) 2.each row treated as record_buffer . repeated for the first 4 rows. and the n summing the 4 result values. Both did not work. – Aiolos Apr 27 '17 at 10:21

1 Answers1

0

Problem was with the client providing outdated files. The code works just fine.

Aiolos
  • 28
  • 6