Brendan Gregg


Algorithm::Hamming::Perl - Perl implementation of ECC Hamming encoding, for single bit auto error correction.


use Algorithm::Hamming::Perl qw(hamming unhamming);

$code = hamming($data); # Encode $data

$data = unhamming($code); # Decode and fix errors ($data,$errors) = unhamming($code); # + return error count


This is an Error Correction Code module, implementing Hamming encoding (8 bits data, 4 bits Hamming - ie increases data size by 50%). Data can be encoded so that single bit errors within a byte are auto-corrected.

This may be useful as a precaution before storing or sending data where single bit errors are expected.

Hamming encoding was invented by Richard Hamming, Bell Labs, during 1948.


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.


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 time. Only use this if you are encoding more than 1 Mb of data.


   perl Makefile.PL
   make test
   make install




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);


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.


Try not to join Hamming encoded strings together - this may give results that look like a bug. If an odd number of input byes is encoded, the output code is short half a byte - and so is padded with '0' bits. Joining these with a string concatenation will contain the padding bits that will confuse decoding.

The above problem can occur when inputing and outputing certain lengths to filehandles. To be safe, my example code uses a buffer of 3072 bytes - a safe size to use with filehandles.


Copyright (c) 2003 Brendan Gregg. All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself


Brendan Gregg <> [Sydney, Australia]