SumBuff
(Engine-Level Function)
Description: | Returns the summation of bytes in a buffer. |
Returns: | Numeric |
Usage: | Script or steady state. |
Function Groups: | Generic Math, String and Buffer |
Related to: | BuffOrder | BuffRead | BuffStream | BuffToArray | BuffToParm | BuffToPointer | BuffWrite | Sum |
Format: | SumBuff(Buffer, Offset, N, Increment) |
Parameters: |
Buffer |
Required. Any text expression giving the buffer to sum. |
Offset |
Required. Any numeric expression that gives the buffer offset from 0 to start the sum. |
N |
Required. Any numeric expression that gives the number of bytes to sum. If N is negative, the absolute value of N is used, but the operation is changed from summation to XOR (each successive byte is XORed with rather than added to the first byte). |
Increment |
Required. Any numeric expression that gives the incremental step of the sum in bytes. |
Comments: | This function returns the 32 bit sum of N bytes in Buffer, starting at Offset and stepping by Increment bytes. This function is useful for computing checksums for serial communications. |
Examples:
Given that myBuff is length 256, and values are the bytes from 0 to 255:
sum1 = SumBuff(myBuff, 0, 256, 1);
sum1 would add 0 + 1 + 2 + ... + 254 + 255
sum2 = SumBuff(myBuff, 5, 100, 1);
sum2 would add 5 + 6 + 7 + ... + 103 + 104
sum3 = SumBuff(myBuff, 1, 100, 2);
sum3 would add 1 + 3 + 5 + ... + 197 + 199
To compute a 16 bit checksum which adds 16 bit words in a 100 byte buffer with the low byte first:
serCheckSum = And(SumBuff(buff2, 0, 50, 2) + 256 * SumBuff(Buffer, 1, 50, 2), 0xFFFF);