I'm using AIX 4.3.2 with ksh and would like to set up an alphanumeric paging script which is compatible with the New Zealand Telecom TAP protocol, which uses a checksum of the message to verify its accuracy. To achieve this, I would like a script (perferably perl) which performs the following:

1. Take as input, a file which may include printable and "unprintable" characters.

2. Produce as output, a number which is the sum of the ASCII values of all the charaters in the file.

Eg: If the file contains:

123\n

A\n

This would add up as follows:

Char ASCII

1 49

2 50

3 51

\n 10

A 65

\n 10

Total: 235

So, the output should be 235 (decimal).

If you've done that, here's an optional part for extra points:

1. Take the last 12 bits of the above.

2. Convert each group of 4 bits into the ASCII chars 48 ("0") to 63 ("?").

Eg: In the case of a checksum like 12667 (decimal), this converts to binary:

11 0001 0111 1011

The last 12 bits are:

0001 0111 1011

Or in decimal:

1 7 11

And you can just add 48 (decimal) to each of these to get:

49 55 59

Which can be represented by ASCII chars:

1 7 ;

So the output should be:

17;

I will probably "reject" non-perl answers until I give up waiting for perl answers, since perl is my preference.

Thanks.

#!/usr/local/bin/perl

$file = "file.bin";

open(F, $file);

$/ = undef;

while(<F>)

{

$sum = 0;

for( $i=0; $i<length($_); $i++)

{

$sum += ord( substr($_, $i, 1) );

}

}

# Now checksum in $sum;

$t1 = $sum & 15 + 48;

$t2 = $sum & 240 + 48;

$t3 = $sum & 3840 + 48;

$result = chr($t3) . chr($t2) . chr($t1);

print $result;