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.5-pp37-pypy37_pp73-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 651de56bfe85bda61701c9e508ac33af36002447d6a47648635255e97477edf7 |
|
MD5 | e4641ccde0ce878f0897b0449958b671 |
|
BLAKE2b-256 | 446b4629184cfdc49354eb5d60554b0c1f67a86eb8297ae0c0c4a29df7a106a1 |
Hashes for hamming_codec-0.3.5-pp37-pypy37_pp73-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f40fb47b28e6c1bb7210e55ad99f1da77e98cf9c0daf3cde416ddb8d9d33f7e |
|
MD5 | 8f4aa2ad21bd50f133f49326754ff613 |
|
BLAKE2b-256 | e92e63ee51f033f5b3649c09c35f6eb8771432519c46af33fd6f1cf7202cca95 |
Hashes for hamming_codec-0.3.5-pp37-pypy37_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9536e058e7c215ae93594af4884fcbf978c97e58d8f0796b70b2feb8ffc02ece |
|
MD5 | 7b37ab32394f8beda89319d853d27643 |
|
BLAKE2b-256 | 7ac2d244670c8af08b6b4dcc81c70d05717acd85977af693e5b69ac9e041bcf1 |
Hashes for hamming_codec-0.3.5-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0173490a4372f00f07027d82dd7499a4752c8008dd3d74c513d05bb2e082cf87 |
|
MD5 | e351c019dfd761c4184a2a993ae35473 |
|
BLAKE2b-256 | c9307b84ff5c908332333fde931fd50e6a9a6747a173dcfe89ff06476df59949 |
Hashes for hamming_codec-0.3.5-cp310-cp310-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40c4e5f615a8c24604f6dfbc6bd653a7843c8e8ccb36d1505f58ca80bfcd4f83 |
|
MD5 | f73f83945c01558b2f7d2eff843c7106 |
|
BLAKE2b-256 | 6403c04fab59db98d032031950ba98e74462f4d522996994d0e037002c94aa36 |
Hashes for hamming_codec-0.3.5-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55f7006274912eef139215b1b22fbd9d26548334b8fc023fcbd2fca7062c7486 |
|
MD5 | cfa3e979c0b087e548a521bbd149dda0 |
|
BLAKE2b-256 | 075bad1b81a22ea7ceac78f00b8469a1588dd2bebe69b0dc911a335d9bc01b9f |
Hashes for hamming_codec-0.3.5-cp310-cp310-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c8ebe1ec78732d67cfb58fff0583afc6401b341484568e2786b21e8202d1003 |
|
MD5 | b7ad7919ab392c988bace8396b20c4cd |
|
BLAKE2b-256 | 4f3d7d4d049dc6e7587fa2d34d05d98d188ad36e6893cc67a5036e748880e369 |
Hashes for hamming_codec-0.3.5-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0f904509871fdc866d468984ebabdde74f4417c8097c5f15194f13ec16bc8e5 |
|
MD5 | b6c4eb7f1ef31dee73fbb0e8c620adf2 |
|
BLAKE2b-256 | 784f9ba04f5da286e517c0330fda12f4ec9302d6991eb4756cdc9e6f992344ac |
Hashes for hamming_codec-0.3.5-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17d1d1401cd21fdd32b059f48d06dd907eac19ce06df68f8d44fe15e34442b36 |
|
MD5 | aa86fd469774d2c19331a5cbbebb183a |
|
BLAKE2b-256 | 056ad05640faaec934a3998b19deef4fcbe562004c094a8b62e4750df69b9a77 |
Hashes for hamming_codec-0.3.5-cp39-cp39-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f32fabc63b40377c89e1bca08b4b52fbf235a4f3745fcf57e4508b11ed7356d6 |
|
MD5 | 3605460654e4c0fcd7bb3b5b7a3b2635 |
|
BLAKE2b-256 | 9b9eb11f91f1daaea3258a8a54e28a9d1f6b8a2a174f42a280c51368c768961c |
Hashes for hamming_codec-0.3.5-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 422635e75a8317d4b05c4a626e82af181948ee3ec765522a72734e1917c82955 |
|
MD5 | e7bf43965a074537ab063df467199f8f |
|
BLAKE2b-256 | f5749e109e0033de9070855f7da027581c5be77fb9679edefa9d80b065ce0c2d |
Hashes for hamming_codec-0.3.5-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89fb9ee661a22bd610db0db4225b3499de94a78fcc5bbf814ca53d11db3bcc71 |
|
MD5 | 2ce431c44bdd818fbda87e72e54ce81c |
|
BLAKE2b-256 | 4f386183dafd4fb64f0a898b8286014b9c957a25f325fbf3ce2711eed51ce9a2 |
Hashes for hamming_codec-0.3.5-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b08ecc7cd2105b81803d83839684c23cfcc8eb42600545a657b6915d632cd69 |
|
MD5 | fc27adb07303facd33670ee41c472c84 |
|
BLAKE2b-256 | 38081f5645e5f4d16b4a1ab6f6d38cb3d8c0231ddaab7983323df729808c8ac0 |
Hashes for hamming_codec-0.3.5-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c47d599cccc8f77bca7bfbbec4e2083a1e01ac0340d35e19b46a2eaa92850dd |
|
MD5 | 6f0baa2ba569237e4d3a32c563750b63 |
|
BLAKE2b-256 | 1f94758a327be6ca221464e3b667cb2142e69f8abb0d9b17aa165ad0e4a08d64 |
Hashes for hamming_codec-0.3.5-cp38-cp38-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb40f64721b75fe1a115ab0463eda8eaa79a7380e38ac21d48f7f29b072c47ab |
|
MD5 | cf3bb6edc0c12fc552a34028a1c31d6e |
|
BLAKE2b-256 | 3a95612dce58b6c687a39f782ae1eb4052aa37dc2f7cfd36eafb13388d5f7765 |
Hashes for hamming_codec-0.3.5-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | edcd62e37cc85c93f0785f93a88a4c613541751ea810a6d29c0ca48d4485adeb |
|
MD5 | ceec89953e198575e93444f50b6b7224 |
|
BLAKE2b-256 | d88c47596a35ddc0f4f6858dd2f687adf44a3d3b7380e02cebd43db28ee553dd |
Hashes for hamming_codec-0.3.5-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6353279551a042598cddce59a2048bd270450569d3a1f7cb2f4ad01995d632b |
|
MD5 | f96fe6c916a4c5e5d81cb334259ee866 |
|
BLAKE2b-256 | f8a45f3dd4984864c210c5bc254ca6a3e3e5f3177d17d489a4ea7759e3af4184 |
Hashes for hamming_codec-0.3.5-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d8e17d30796b74ce02dfc3a1764ab722163f04e18c54136c5a51507e18b1fbb |
|
MD5 | 10546143e058c8bd9edff9757bd1b610 |
|
BLAKE2b-256 | bf0518495af579a0e4b68e8cbde18707fac57f945b4618d386acaa649057322e |
Hashes for hamming_codec-0.3.5-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91cb503983b7ebc043a23c992cb4fbd5abffdc7f2dc165959f3a8ab6c424533b |
|
MD5 | c1c24e4d28c76a9138a4b9abbccbec7d |
|
BLAKE2b-256 | 8e1c24379189ebe189a75542a14bcb284fd1193447b6fce9ca37cc1c9e760a3b |
Hashes for hamming_codec-0.3.5-cp37-cp37m-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8748cafe95ee700d1155289fefafc93e018fd3611a18a299b6f63940992a6e61 |
|
MD5 | 96ae68a339ddf51689f9b0615e22e7f9 |
|
BLAKE2b-256 | a4b25d649c51357e0bca125fdc40f251a8756260f29982ab393906d90fcb4448 |
Hashes for hamming_codec-0.3.5-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb86f69a3bb0bfd2a0d82b01a1a77edfccb69eee50b5ab7c6bf287da58e39ed3 |
|
MD5 | d276eb939133c65536817199f6c32ff0 |
|
BLAKE2b-256 | 7291ad31b8c9e876e9ef99683e2d735ab4621fe064649952f9c8ed1e66fbed6e |
Hashes for hamming_codec-0.3.5-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 480428f6bbfa90df638779050eaab28bb34ef43e43919162bb3f8da0e58ad237 |
|
MD5 | 8c606eee3c76961550f30f48afbd92d6 |
|
BLAKE2b-256 | fc824a2b793276f11aa45698c2ec33984f5dc502ef177cda5d903c85175bd7c2 |
Hashes for hamming_codec-0.3.5-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0bf238578aaf76617330535d7c58fe32224f4ff968a47bb9e70411e1fdd55377 |
|
MD5 | 25d0e7cc8dbe199d42542dda71e9322c |
|
BLAKE2b-256 | b43e4bdd7c3d570e4e5e3ea2165a5e2b6f6d2a6e267bcea18496c3c7652dd058 |
Hashes for hamming_codec-0.3.5-cp36-cp36m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 903d815b6868407df1723420517ef8a8b245ba441044f931570510bb8ea6ec6b |
|
MD5 | 84a394e472f661371a917ed89d63bbd7 |
|
BLAKE2b-256 | f70645a91e752bcd772aaf3310e6346e2f5c7b1700682ddb68713b742fc48334 |
Hashes for hamming_codec-0.3.5-cp36-cp36m-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c37a611b1bc7eb30ab6a61cc1a7a7205bce98f2746d16725e1ea08e3db457a22 |
|
MD5 | 1fcac7a01016c1ced697d3b353973f7a |
|
BLAKE2b-256 | a31a95dc935e707f923ebf2c5232495fb384622749361fa559344f0ad377a897 |
Hashes for hamming_codec-0.3.5-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78f8cd732ee419dc5d703ffb9d8430c8dd1535e5cd6d68324c46fd658444e300 |
|
MD5 | 9db6773ca84a3a4d3e8f5da29d0e61b6 |
|
BLAKE2b-256 | 12f7353bb5bc8ef9b601b52ffbb1cde29d711b32c7dc0152605dbc6f603d8abe |
Hashes for hamming_codec-0.3.5-cp36-cp36m-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13abee1efc64c4641d51945b46e1a636e2ac1aabbe4ad9e8a7c0c7c1aa40a967 |
|
MD5 | b08c74233866f67167e909cd0fb5787b |
|
BLAKE2b-256 | 48b156fb6565e136b8c96b21e8f1c3994b79938ecbe6c6b92f60e812243ed1f7 |
Hashes for hamming_codec-0.3.5-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a83959c8a1f40b861337d225eb217b09ada685dec16415b2ce9390e3a6cab195 |
|
MD5 | a46a1a55e09b0110cb25d9fdaf020964 |
|
BLAKE2b-256 | a66bd3d35c929d93e025bedf49ded7f5e2835669ee6d263d827c7bb1352ca4cf |