Package: hamming-codec
hamming-codecSimple encode/decode utilties for single-bit error correcting Hamming codesTable of ContentsRequirementsInstallationPythonC++Python UsageCommand-line UsageImporting thehamming_codecPython ModuleC++ UsageIncluding in your ProjectExamplesError-correctionReferencesRequirementsTested on Python>=3.6and requires C++ compilers supporting C++17 features.InstallationPython InstallationInstall usingpipas follows:$python-mpipinstallhamming-codecC++ InstallationIf you wish to use thehamming-codecencoding/decoding from within C++,
one needs to include the single header filesrc/cpp/hamming_codec.hin their project.
There are a few ways to do this:Copy the filehamming_codec.hinto your project's include path.Addhamming-codecas a sub-module (or equivalent) and use CMake to expose theHAMMING_CODEC_INCLUDE_DIRSvariable by callingadd_subdirectory(/path/to/hamming_codec)to your project's CMakeLists.txt file.
Seesrc/cpp/examples/CMakeLists.txtfor an example.Following either approach, you should be able to add#include "hamming_codec.h"to your
source code.Python UsageFrom the Command-lineAfterfollowing the Python installation, a commandline utilityhammingwill be available to you, which can be used for simple encoding and decoding
of data words. Usehamming --helpto show the full set of options and commands.EncodingYou can use thehammingcommandline utility to encode messages of specified length
(in number of bits) as follows:$hammingencode0x1234160x2a3a121Which shows that the 16-bit message0x1234is encoded as a 21-bit word0x2a3a1.DecodingYou can use thehammingcommandline utility to decode messages of specified length (in number of bits) as follows:$hammingdecode0x2a3a1210x123416Which shows that the 21-bit encoded message0x2a3a1is decoded back into the 16-bit word0x1234.Importing as a ModuleOnce you haveinstalled hamming-codec, you can import thehamming_codecmodule and perform encoding/decoding like so:>>>importhamming_codec>>>encoded_message=hamming_codec.encode(0x4235,16)>>>print(encoded_message)010001010001110101100>>>hex(int(encoded_message,2))'0x8a3ac'>>>decoded_message=hamming_codec.decode(int(encoded_message,2),len(encoded_message))>>>print(decoded_message)0100001000110101>>>hex(int(decoded_message,2))'0x4235'C++ UsageIncluding in your ProjectFollowing thesteps to addhamming-codecto your include path,
you can encode/decode messages as follows:#include"hamming_codec.h"...uint32_tn_bits=16;uint32_tinput_message=0x4235;std::stringencoded_message=hamming_codec::encode(input_message,n_bits);std::cout<<"Encoded message: 0x"<<std::hex<<std::stoul(encoded_message,0,2)<<std::endl;// prints "Encoded message: 0x8a3ac"std::stringdecoded_message=hamming_codec::decode(std::stoul(encoded_message,0,2),encoded_message.length());std::cout<<"Decoded message: 0x"<<std::hex<<std::stoul(decoded_message,0,2)<<std::endl;// prints "Decoded message: 0x4235"C++ ExamplesAfter following thesteps to build the C++ library, you
can run the C++ examples. For example,$./build/bin/example_encode0x4235160x8a3ac21Single-bit Error CorrectionThe Hamming encoding algorithm used withinhamming-codecallows for single-bit error corrections. That
is, during the decoding process, errors in which a single bit has been flipped in the encoded message
can both be detectedand corrected.For example, if we flip a single bit
in the encoded messagefrom a previous sectionsuch that the 21-bit word0x2a3a1becomes0x2a1a1,
you will get the same decoded message as before:$hammingencode0x1234160x2a3a121$hammingdecode0x2a3a1210x123416$hammingdecode0x2a1a121# flipped a bit0x123416# ...but decodes the same as before!ReferencesFurther information about the Hamming encoding algorithm employed withinhamming-codeccan be found in the following resources:3Blue1Brown's "Hamming codes and error correction"Ben Eater's "What is error correction? Hamming codes in hardware"tutorialspoint's "Error Correcting Codes - Hamming codes"Wikipedia entry for "Hamming Code"Wikipedia entry for "Linear error-correcting codes"