explanation of function

th0811

New member
Joined
Jun 15, 2022
Messages
2
Programming Experience
1-3
Hi all,

I am not familiar with C# at all and in fact only familiar with VBA. I am trying to translate a C# function into VBA so i can use it. The high level function purpose is

  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 the end of the line character), padded with nulls if required, and exclusive OR (XOR) them into the result variable
with the pseudocode as
C#:
num_chars = strlen (record_buffer)
FOR (i = 0;i < num_chars;i++)
  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
and the function as
C#:
var result = 0;
var num_chars = record_buffer.Length;

for (int i = 0; i < num_chars;i++)
{
  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;
}
my question is if a simple string was fed to this function with no footer i.e TEST22 would the function be testing each letter to generate the Checksum value as i dont understand what it means by : Break each record into four byte (character) sections

Apology if the nature of the question is a stupid one. But any simple explanation of what its doing would be much appreciated.

Regards

Tony
 
Last edited by a moderator:
Unfortunately, you'll need to give us more details about the type of the C# record_buffer.

In C#, characters are 16-bit values (two bytes), but the code seems to be something that was translated from C or C++ where characters are 8-bit (one byte).
 
Last edited:
Assuming that the string is "TEST22", and all the characters are in the 7-bit ASCII range, then your code above will process all characters and come up with a "checksum" that would be the same as the VBA, C, or C++ code.

So answering your question strictly: "would the function be testing each letter to generate the Checksum value"? Yes it is processing each character, but the checksum you get back may not be the same as the VBA, C, or C++ version.

Now if your question was: "Is the function breaking the string into 4 byte sections?" The answer is no because each character in a C# string is 2 bytes. So record_buffer[i] will be returning a 16-bit value, not an 8-bit value.
 
thank you both for your comments,

Now if your question was: "Is the function breaking the string into 4 byte sections?" The answer is no because each character in a C# string is 2 bytes. So record_buffer will be returning a 16-bit value, not an 8-bit value.

would this then mean that the string TEST22 would be broken down into "TE" to give 4 bytes and then "ST" and then the XOR carried out.

this may be too much for me
 
Back
Top Bottom