Simple encode/decode utilities for bit-error correcting Hamming codes
Project description
hamming-codec
Simple encode/decode utilties for single-bit error correcting Hamming codes
Table of Contents
Requirements
Tested on Python >=3.6
and requires C++ compilers supporting C++17 features.
Installation
Python Installation
Install using pip
as follows:
$ python -m pip install hamming-codec
C++ Installation
If you wish to use the hamming-codec
encoding/decoding from within C++,
one needs to include the single header file
src/cpp/hamming_codec.h in their project.
There are a few ways to do this:
- Copy the file hamming_codec.h into your project's include path.
- Add
hamming-codec
as a sub-module (or equivalent) and use CMake to expose theHAMMING_CODEC_INCLUDE_DIRS
variable by callingadd_subdirectory(/path/to/hamming_codec)
to your project's CMakeLists.txt file. See src/cpp/examples/CMakeLists.txt for an example.
Following either approach, you should be able to add #include "hamming_codec.h"
to your
source code.
Python Usage
From the Command-line
After following the Python installation, a commandline utility
hamming
will be available to you, which can be used for simple encoding and decoding
of data words. Use hamming --help
to show the full set of options and commands.
Encoding
You can use the hamming
commandline utility to encode messages of specified length
(in number of bits) as follows:
$ hamming encode 0x1234 16
0x2a3a1 21
Which shows that the 16-bit message 0x1234
is encoded as a 21-bit word 0x2a3a1
.
Decoding
You can use the hamming
commandline utility to decode messages of specified length (in number of bits) as follows:
$ hamming decode 0x2a3a1 21
0x1234 16
Which shows that the 21-bit encoded message 0x2a3a1
is decoded back into the 16-bit word 0x1234
.
Importing as a Module
Once you have installed hamming-codec, you can import the hamming_codec
module and perform encoding/decoding like so:
>>> import hamming_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++ Usage
Including in your Project
Following the steps to add hamming-codec
to your include path,
you can encode/decode messages as follows:
#include "hamming_codec.h"
...
uint32_t n_bits = 16;
uint32_t input_message = 0x4235;
std::string encoded_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::string decoded_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++ Examples
After following the steps to build the C++ library, you can run the C++ examples. For example,
$ ./build/bin/example_encode 0x4235 16
0x8a3ac 21
Single-bit Error Correction
The Hamming encoding algorithm used within hamming-codec
allows 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 detected and corrected.
For example, if we flip a single bit
in the encoded message from a previous section such that the 21-bit word 0x2a3a1
becomes 0x2a1a1
,
you will get the same decoded message as before:
$ hamming encode 0x1234 16
0x2a3a1 21
$ hamming decode 0x2a3a1 21
0x1234 16
$ hamming decode 0x2a1a1 21 # flipped a bit
0x1234 16 # ...but decodes the same as before!
References
Further information about the Hamming encoding algorithm employed within hamming-codec
can be found in the following resources:
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for hamming_codec-0.3.4-pp37-pypy37_pp73-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82726295f196da465157d0dab6ac034e0bdc5e7c7b6214d3ecfd75872f7c1058 |
|
MD5 | aa02477dcd9bd3a33811d3659648e47a |
|
BLAKE2b-256 | 9eedaa0e9af1f7680f17014ae73b10534180118dcdc7225584c2a1a7b91a9196 |
Hashes for hamming_codec-0.3.4-pp37-pypy37_pp73-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16b7507f50ed6ab26db22695549bbceac80af14b89daf606cf358d0d35d8fa79 |
|
MD5 | d546350c0d86dd008b3c88ba598e7ab0 |
|
BLAKE2b-256 | 89aa2318e9e8c52da53f1c370143c4667973f649a6511504d441150efdf8d0c9 |
Hashes for hamming_codec-0.3.4-pp37-pypy37_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dcd6e18f95cebc79df8aa96996d5120ce76aa2ee5445645e2543f7d9dcb27682 |
|
MD5 | c032ac8b4813f0966509e913a0bd2d2a |
|
BLAKE2b-256 | c691eadd5781bde8df2628e8c29e21cc574e5f5aa56853d720d05bc63ee7343f |
Hashes for hamming_codec-0.3.4-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2fd2047f0d3e2744dc19988719c2787d1c0f9386f44d157925085455115b5d0 |
|
MD5 | bbe59b3c25680da478d7a0610b4d7d27 |
|
BLAKE2b-256 | b8f73cfa897b2519a55be198fed56e902aec32237022c8dc81aa5d8c9777fb2a |
Hashes for hamming_codec-0.3.4-cp310-cp310-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48704a4fd779b44b638a6c024b1ca8e5ffcd936fe71a941518d05229c426da86 |
|
MD5 | cf8a1c3732d4c87c226f6c5b083bcc03 |
|
BLAKE2b-256 | 025cb682555d8578af0a3dd81dc2966e5bedbd20994c9707fc30600b3d562768 |
Hashes for hamming_codec-0.3.4-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a04a44a25bd5e0f13b5293a01b1fd681dcd46c23de566455865eed64a1d3f5b8 |
|
MD5 | 877893a99300c9c94bcd5bc26d6ec8fb |
|
BLAKE2b-256 | ea28bcb34d7bb105425e5512de62892963c8b1b383289a49d54957eddee5dbfe |
Hashes for hamming_codec-0.3.4-cp310-cp310-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f4942542b6c8e1ac28b2259bed8198615cbc50f5c7f722d58b5d53fa218c0515 |
|
MD5 | e8903bef8a631ea057a8e6e942eb4715 |
|
BLAKE2b-256 | 07b9a1dbfc42eb711474056f17a12d38dee3e347ca1e600147563ecf990bc420 |
Hashes for hamming_codec-0.3.4-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 258f869e908c5b359cc36e0e97478bf137414a2d0974e40c1adc154ac1d793c9 |
|
MD5 | 514aa8a4584863b0feaba4cebc0bfb13 |
|
BLAKE2b-256 | 9d6ef03d5cf67cd8451c813398d9e2dd2fb3f252dcc8da80611f8975daa839bf |
Hashes for hamming_codec-0.3.4-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5b3f93e9120e71133459662b0bf84233248175fba1ca177bfde95fd876747b3 |
|
MD5 | bd6396a82f3fe39143d4612b918fbd2c |
|
BLAKE2b-256 | 917dcf6dfe8913ac3c5c976390373d8017764f2b5a2b899a81812eb1079f32f2 |
Hashes for hamming_codec-0.3.4-cp39-cp39-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c05fe661a5782bc10c0b205386af00250aba807813853a754e2703d87eac19f9 |
|
MD5 | 847fba9324dc13363d8db317f0b87dfd |
|
BLAKE2b-256 | d842ddecd64b40391ebb79877f6f1bfce487fc73e8a9b9655bc5a8ce6c6a11c5 |
Hashes for hamming_codec-0.3.4-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16554dcb0d4ab18ec0122d778765f494d531d3e885e17c412f9de57f40dea126 |
|
MD5 | 490d6ff1e3359929f57965d8935464c3 |
|
BLAKE2b-256 | 75b3d28d4e2084e7c273d166efe16d4b1ad77508da21126c5faf02edb870371b |
Hashes for hamming_codec-0.3.4-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 659af2bf79eda546e711d3e8d6f5904fb33988de40ea526e7de239d148358db6 |
|
MD5 | 276d113e1fd5ca48ca33a8985462d3ff |
|
BLAKE2b-256 | b6689c400486f9cd15abf1544d00cc5383aff356d2edcf776e1409ce3e88aae3 |
Hashes for hamming_codec-0.3.4-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a63ab732d7352680c8cdbd2d65b4e1a01f8a36866617598d6cc96be6b24ece95 |
|
MD5 | 3e3436898f63c77f138a1bfc019fc047 |
|
BLAKE2b-256 | 611361111313830f27ef39b8e93ba3ecf2ac488c6dfea4986622d1edfa537437 |
Hashes for hamming_codec-0.3.4-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66b2110b099e20d5b5030d813331567c998e7141c45d93a48faee8e1c12578d6 |
|
MD5 | cb4afed5e578354162f0ac34bdb46713 |
|
BLAKE2b-256 | f213ed0a3310c7eab17faa88f307b6604c687be66aa360ee49b5852a302a65e9 |
Hashes for hamming_codec-0.3.4-cp38-cp38-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57370cc6058985a667ec6e1f9a35ee979e0138feccba6c330634fe57632d6def |
|
MD5 | b848b40244a99596a45a84eb451f53d3 |
|
BLAKE2b-256 | 08d1b0e3734160a3761c051f5a2d429b96ead9fdf1daaafcab94b239182e7482 |
Hashes for hamming_codec-0.3.4-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2baa0e72f8103c709b12b34bff5e60d8ea713c3f3d8805e0c7390062ac643fb6 |
|
MD5 | 66397c7b36fe6f2ef786bfa680f1e965 |
|
BLAKE2b-256 | f3caf7f412e0d9479d88cf5f49330d90c8655457327394cb92828a0d063a9aeb |
Hashes for hamming_codec-0.3.4-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 19675f0c5bf64f7deaaaa40e29b5d2485fb13f7cc0c9bf6d312da78f7a625e60 |
|
MD5 | 378ee5b015f56c4572236f4fae9a3a86 |
|
BLAKE2b-256 | fe4e443f39788bab25546191c7d85d566cba9cccb07a343d27de297bb28c2a29 |
Hashes for hamming_codec-0.3.4-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56adc0ef893b37e4551a077fccaca32f58dc7469d16e62358bc79813af58f87d |
|
MD5 | a6dc4d23565fa69752557e81702b6e44 |
|
BLAKE2b-256 | b0b1a52d9a04480bbc163415e19aa1973d94a411ae313d0ceda33fdaf9b7e6da |
Hashes for hamming_codec-0.3.4-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 484ca73d77563dc3628f5ab507bfaec4aa88e1158c303430799b0c2af275a112 |
|
MD5 | 42fd5727285ac08de6c3498f5de84d56 |
|
BLAKE2b-256 | d03c51adc29749d3dccedd5c526c8a93b6124a06aced8ef9d0ed68839e656563 |
Hashes for hamming_codec-0.3.4-cp37-cp37m-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77364dc0d5ddecbfd7fb9a65d81962fe86cdbb7f985d3c0cf875f2fb53aaa7d3 |
|
MD5 | 99ed25758b43c4a35cde40edd8e77687 |
|
BLAKE2b-256 | c4121f95352f1946b216bdad9ba0cc2fc750f2b1c48c42b56a7ef5298121aad4 |
Hashes for hamming_codec-0.3.4-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc6256b8bdd01cc8ab1862b4f4d15fb01c36306cc235b837548e9b4315af1727 |
|
MD5 | 797987b1ac3133d52941909a1e5e460a |
|
BLAKE2b-256 | d31005c94aca5285e3f7dd3f05ef80137280b9c94116207139392e513cdc86b5 |
Hashes for hamming_codec-0.3.4-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ead3899cb5f6318b15f1a1aa64f423821dd233b0b1a34985672888c932dfc1e |
|
MD5 | b88df9ad3497edc3ddbe7f51514828d3 |
|
BLAKE2b-256 | f23398689f6c9b207be2a7f7edb440b58eb231a7a3a1cd521bed803dcd3d4a13 |
Hashes for hamming_codec-0.3.4-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8278f11b510100633e6704725ac388c8fc79a0e5c9fbb27744dd9026e95bf6d6 |
|
MD5 | a23d62ec22ff2f59bf03a8bcea44ee5e |
|
BLAKE2b-256 | 37b4cd8309e7e4d614bab1b6bc24ad54a1350a666be38e34ff6f2429b3359ac0 |
Hashes for hamming_codec-0.3.4-cp36-cp36m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f39a04d440e87a5afc2e57eaaa945bd892ca6f30e8e548c26e2e3ced76c45bd |
|
MD5 | 62b742142e4017e29007e1b2dcbae50f |
|
BLAKE2b-256 | 3e659e02b820e72b2769262888aab6c11ab58b9515cd4dc805012e17e12d768c |
Hashes for hamming_codec-0.3.4-cp36-cp36m-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2860ff10cf8b7ab19bcb78adbb84ca79c9c1dbefd6bfeba0db635ce435e75ba |
|
MD5 | dcd5be07aa5a477d9f91d79eeda40baa |
|
BLAKE2b-256 | c68bbb08591c10169f50d9ae54f856b6c1a2069b19230f080bf0b75da2c37ae6 |
Hashes for hamming_codec-0.3.4-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f111f181db631ec1f47881196822051593a4dca87291f0de6b68f471af7a0be |
|
MD5 | 5ad32c77014829bf3942d53f569f047c |
|
BLAKE2b-256 | e7240d75c00be9ceeebf7078b5bb619d93f1f4399006dbb36bff91ecb4de33ee |
Hashes for hamming_codec-0.3.4-cp36-cp36m-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4eea75fa79c652ef1e805a2156eec92e6a2f97712b7312b1c1470119f289c4a |
|
MD5 | 21ebaf5f8075cc6dc3d956a72fab8cb7 |
|
BLAKE2b-256 | 61f8c51d7f30da001a345fd4a1b0ac1a515b7ecf1173d1ffc32c7114051fb11c |
Hashes for hamming_codec-0.3.4-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c01f62326ae36ffa403948c9f417c9d08ce0e88798e0490ace931c49723fbd5 |
|
MD5 | d73979d14111cf3009aadc00f8843ef2 |
|
BLAKE2b-256 | 2948cddbcdd22cd46e2d2192bcd8f25046de083251015d1e0c9e9e1de7d64216 |