Algorithm-Hamming-Perl
view release on metacpan or search on metacpan
Revision history for Perl extension Algorithm::Hamming::Perl.
0.05 Fri Oct 17 22:43:33 2003
- original version; created by h2xs 1.21 with options
-A Algorithm::Hamming::Perl
} else {
#
# Slow method
#
### Fetch the 2 Hamming codes
$ham_text = unpack("B*",$chars_in);
$ham_text1 = substr($ham_text,0,12);
$ham_text2 = substr($ham_text,12);
### Convert each code into the original byte
($char_out1,$err) = unhamchar($ham_text1);
$err_all += $err;
($char_out2,$err) = unhamchar($ham_text2);
$err_all += $err;
### Add bytes to output
$output .= $char_out1 . $char_out2;
}
}
# Decode leftover bytes (if present).
#
if ($length == ($pos + 2)) {
### Fetch the 2 leftover bytes
$chars_in = substr($data,$pos,2);
### Fetch the Hamming code
$ham_text = unpack("B*",$chars_in);
$ham_text1 = substr($ham_text,0,12);
### Convert the code to the original byte
($char_out1,$err) = unhamchar($ham_text1);
$err_all += $err;
### Add byte to output
$output .= $char_out1;
}
return ($output,$err_all);
}
=head1 EXPORT SUBROUTINES
=over 4
=item hamming (SCALAR)
Returns the Hamming code from the provided input data.
=item unhamming (SCALAR)
Returns the original data from the provided Hamming code. Single bit errors
are auto corrected.
=item unhamming_err (SCALAR)
Returns the original data from the provided Hamming code, and a number counting
the number of bytes that were corrected. Single bit errors are auto corrected.
=back
=head1 OTHER SUBROUTINES
=over 4
=item Algorithm::Hamming::Perl::hamming_faster ()
=head1 EXAMPLES
See the example perl programs provided with this module "example*".
An encoding and decoding example,
use Algorithm::Hamming::Perl qw(hamming unhamming);
$data = "Hello";
$hamcode = hamming($data);
$original = unhamming($hamcode);
=head1 LIMITATIONS
This is Perl only and can be slow. The Hamming encoding used can only
repair a single bit error within a byte - ie if two bits are damaged within
the one byte then this encoding cannot auto correct the error.
=head1 BUGS
Try not to join Hamming encoded strings together - this may give results
data where single bit errors are expected.
Hamming encoding was invented by Richard Hamming, Bell Labs,
during 1948.
EXPORT SUBROUTINES
hamming (SCALAR)
Returns the Hamming code from the provided input data.
unhamming (SCALAR)
Returns the original data from the provided Hamming
code. Single bit errors are auto corrected.
unhamming_err (SCALAR)
Returns the original data from the provided Hamming
code, and a number counting the number of bytes that
were corrected. Single bit errors are auto corrected.
OTHER SUBROUTINES
Algorithm::Hamming::Perl::hamming_faster ()
This is an optional subroutine that will speed Hamming
encoding if it is run once at the start of the program.
It does this by using a larger (hash) cache of
preprocessed results. The disadvantage is that it uses
more memory, and can add several seconds to invocation
EXAMPLES
See the example perl programs provided with this module
"example*". An encoding and decoding example,
use Algorithm::Hamming::Perl qw(hamming unhamming);
$data = "Hello";
$hamcode = hamming($data);
$original = unhamming($hamcode);
LIMITATIONS
This is Perl only and can be slow. The Hamming encoding used
can only repair a single bit error within a byte - ie if two
bits are damaged within the one byte then this encoding
cannot auto correct the error.
BUGS
Try not to join Hamming encoded strings together - this may
#!/usr/bin/perl
#
# example02 - example of converting bytes to hamming code, creating
# bit errors, then retrieving the original data.
#
# 18-Oct-2003 Brendan Gregg Created this.
use Algorithm::Hamming::Perl qw(hamming unhamming unhamming_err);
$data = "Hi";
### Hamming
$hamcode = hamming("$data");
( run in 0.244 second using v1.01-cache-2.11-cpan-1c8d708658b )