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. 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.1-pp37-pypy37_pp73-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be3fe79edb3a4423881a0649b3a53975e182cdc29dc2d1834095ff370a32ce75 |
|
MD5 | a6b65a764e303833ad7a12e363215cbf |
|
BLAKE2b-256 | 3cb2f253ce85697f5b1a347ea7b1247ee3bf0509f1f269851d5d6383a266570d |
Hashes for hamming_codec-0.3.1-pp37-pypy37_pp73-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f147e559cc9d95ec38b5f290e1fe702b86641b573760de50908ad32512d7e6df |
|
MD5 | a917303ae9856743f6fcb5c46495b6b0 |
|
BLAKE2b-256 | a8ef1723833d26243e398953781660bd796ccebc5d0ce04d151b275364f52647 |
Hashes for hamming_codec-0.3.1-pp37-pypy37_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02a7b687c496f6cce93a8f4bb72dda13bd7d683d60822ff8aa489402310f1246 |
|
MD5 | 2b3580e99d43c2ca9a06e0c0242f4346 |
|
BLAKE2b-256 | 623fc98fb8936bf7c4c1509342c5d815042d8ba01e559b54c7e0c5d86cdc0f1d |
Hashes for hamming_codec-0.3.1-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d5b80b57bb7fdf571a8e31abc47e536ca620152237c2c3b6f004d257295ddf0 |
|
MD5 | d24b3e293fee4b3bb4c74cb61dceca40 |
|
BLAKE2b-256 | d6da78f8e3ac610c5b7b20961e4987d01b479dd773580eaedc3fa6e9b2dea820 |
Hashes for hamming_codec-0.3.1-cp310-cp310-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d62104b11939197b565da9a234afd74234b2b5345b202aa8914917ca79ae6b76 |
|
MD5 | 0413365724abe10e4822f5167bf54620 |
|
BLAKE2b-256 | f3f547b29a4e6d8dc5d68598bf2cd4303e6c32f0857778c3bf1a25e5c7686ae3 |
Hashes for hamming_codec-0.3.1-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8628483a464fd2c93771dfccec86051f45568ababfc43b821bf280b376b5f17 |
|
MD5 | 7f872e291af2e2ece253b486a6f49de3 |
|
BLAKE2b-256 | acd3f608414b67159e97d2f1ab1a1936cd4fb2266b84018f80af6ce251ae72db |
Hashes for hamming_codec-0.3.1-cp310-cp310-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce9c8bfaa057dfe5ebc85d67164ca60a8586c014f887a274bd9fc490dd34573f |
|
MD5 | 1833c8c8c87f94e1eded3ae480178974 |
|
BLAKE2b-256 | 8809eaba43bc021f21565bf7cef8b95086a39b2e56bff97772fb2813fc3a1f37 |
Hashes for hamming_codec-0.3.1-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52cedcdd3b3c68ef933accd4d2e35fcfeff9ce4b502434da3aee44cd6754ae2a |
|
MD5 | 8b083dbc0200ede8875846f05da1bed8 |
|
BLAKE2b-256 | 66f6442bbee726676f1139ed8c3e24d5a789cabdd35e7797b0cc47096cdfa06f |
Hashes for hamming_codec-0.3.1-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0eaf7a957444477d5ca57f7b13cd2d5cf6cbf2ccb673845c0d068129ed383568 |
|
MD5 | 928f7555c80c91f8e392576652f0e352 |
|
BLAKE2b-256 | ad931fb724318b65a29dc6321dd147eede8019a55f23255eaf7291089e6cb50c |
Hashes for hamming_codec-0.3.1-cp39-cp39-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d2e5cae4eacd176de9aa5edee4204fc1733d9337fd35920cf2e7f4dbadd86d86 |
|
MD5 | a6cd9a14bd79650f0e5a24d5a95ed5da |
|
BLAKE2b-256 | afbea1ed8ebeafdfd60bcbc22597d87b07aed0624258e2eb79f46b527a577208 |
Hashes for hamming_codec-0.3.1-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9aea7ecdcd230be9cd463d4bd4f54127d88cff520619e9601aa4165167923bd5 |
|
MD5 | d41825bd81753954a499a788b7b6c835 |
|
BLAKE2b-256 | 6adcabea2f7ccbfe8066c2510b5f7f9b2a15bc1c63dd58f5e518322df9db97d8 |
Hashes for hamming_codec-0.3.1-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 980f1dd6173c30fc3a0efed143f9b753976cde7eb5dcc563cbc9ef0a90ce93c9 |
|
MD5 | bcf93cb0a60d6872bb6260cbd87a1d1d |
|
BLAKE2b-256 | 37196dbf6ab4ade4c7917c25f0f595d369a886ad248ad121e4a0c8099e2ec0af |
Hashes for hamming_codec-0.3.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40751839f53d76ffcd837ee4ca73710aa5ddf32cecdebfc804726f12652187f1 |
|
MD5 | 230b17afef9c62c8e60516194341101d |
|
BLAKE2b-256 | 42bd2d3a9c58b530f684e06af96e9248ee67b905bb908f8bc765f137d01fb46d |
Hashes for hamming_codec-0.3.1-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e65c6bce25b2a578c9f66c4078bc8a88716f24b16887e5ee1e316c29e227346a |
|
MD5 | 0e0d72e676cb82305eb95960a4d8cfc2 |
|
BLAKE2b-256 | 1a9edef24ebce5a120605f27fa0a691bdaf92b089a8b52d45eaf4d4dbce5ee5b |
Hashes for hamming_codec-0.3.1-cp38-cp38-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9305e01996c736200783d445943451709ecfc7aa58e98aebcee3cbc4c7949e13 |
|
MD5 | f495a1c48b1fca805ee0b80d5e026141 |
|
BLAKE2b-256 | caabfa7d1ba56aa24ba1e0f2e1b22a1334da559698c64249f67866790e701e58 |
Hashes for hamming_codec-0.3.1-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aea1d400664b2b18ad79b301075d4d9fbf94671825b7641eca33eb040e9b59ed |
|
MD5 | 6c2510c1d216a67c0f6efe0e9e2b8bf5 |
|
BLAKE2b-256 | c838902ebe9fcce0d3779f0a061dead5817db8299f750c8625395af276b4e2b7 |
Hashes for hamming_codec-0.3.1-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 435c428d9ff09812ae956cf6518bd0b714b0380b083ec064a4b9a34d78183f28 |
|
MD5 | e0fad51784f259358b18125674b8dcb4 |
|
BLAKE2b-256 | 730233da6bf8b09d366ab2af005f339d118ba12b6b29de0d5b4d90a929bca986 |
Hashes for hamming_codec-0.3.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc2a9622bb9518a8dd6a0d64611c69ad94dd372a69ac71efb60144ffa7392bd8 |
|
MD5 | 6501897e4d8185baccd5fe7b1ddc5d37 |
|
BLAKE2b-256 | 823eb391251ea849f62a64d957d59213dfa108074a85e26ef1f57116a1c0c956 |
Hashes for hamming_codec-0.3.1-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80d6f48bc741502332adbe4345d3d759370f6447caacb48b57709163a5bc2225 |
|
MD5 | 8e77d979c9a9c68418b917ce7b04aa63 |
|
BLAKE2b-256 | d45ba0af4641d10ef425773e87c9664bf5e1df0a10598c7633d7baf096de3707 |
Hashes for hamming_codec-0.3.1-cp37-cp37m-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca4741a553420def1d24d985c9b4fe0b40c72bbcd43305738aa2f765bfabc774 |
|
MD5 | b6a85262a94e2c942c5946ecedef4949 |
|
BLAKE2b-256 | 1cc2581ef56d5bb720f3ee54b76171beb520e90b4fb17003af2383fefd16728d |
Hashes for hamming_codec-0.3.1-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b6ff07aa6d28ac61004814a0a01a7ded474ebd8fc05f3529df81a3884d713c0 |
|
MD5 | caee1dec1eb611bbbadea3849cba14f8 |
|
BLAKE2b-256 | c1f81307f10e90505e14aa9de4c6cc7b93da06a0866878e08cb07f66f54615ec |
Hashes for hamming_codec-0.3.1-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51c5bcdd635345e095cc02ee35e52e8262a7fe622566c09106f4b9210dac3c3a |
|
MD5 | 1da8f2a2a2998adf6f927ede84d1f6ee |
|
BLAKE2b-256 | bff1b9b6f6f7b928cb5e350f2041fbf8b244b7bdbeeff3c6de502a82282ec8e0 |
Hashes for hamming_codec-0.3.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e93bc214749a2fc713a03bcd4738353407f367b62aabf6202feda5e9f4774129 |
|
MD5 | 568112cf46e9c96d394c6bc86e84c74e |
|
BLAKE2b-256 | d89462fadad1ac09e6d17ed4ae4fdf43169dafd2d63eff46c13ef98fb3a7c338 |
Hashes for hamming_codec-0.3.1-cp36-cp36m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ed6f1c59393654f182537ae9319590b8264eb7662498d9ef89904084753b11b |
|
MD5 | a97135ef8aa32e9ad8281e61d81214db |
|
BLAKE2b-256 | fff48475350074eee710d99fcbf9140b8cb8788bd1c6f3c2284b3086e90614e0 |
Hashes for hamming_codec-0.3.1-cp36-cp36m-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68a13e518293f9776a0a97b8d2a5e1c17c4b7007c3452704ceb7203e0f43ac6f |
|
MD5 | 9a056fd655d55e64a586387ce7bad47c |
|
BLAKE2b-256 | 406d8425588afb2a813ff689f52a742dd7d93f6c4116e48a45836fbc822589d7 |
Hashes for hamming_codec-0.3.1-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3eb1ae20caedae1ad8c95aae30d61c1dafde06e022b92a0bf4263ae91bfab61f |
|
MD5 | 40a04ff0bb3d07525f195f46ca5c49d7 |
|
BLAKE2b-256 | f9d8559200e7b8bc4ec30eca0e88d4c6874363b7e2dbf0a455de1552ed025bd5 |
Hashes for hamming_codec-0.3.1-cp36-cp36m-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e0c8ddf5080a2147233c279a8e7e84f6b8d59aff866d0752d07491388c292a2d |
|
MD5 | d817b4e9c136b94fc007b892fb7920d9 |
|
BLAKE2b-256 | cdbf5882321ec77064edc351b3324e5d691b5849077f1c1f00c9b4ca541fbd5e |
Hashes for hamming_codec-0.3.1-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | caf2f4eb46b7d46865dd1e71280572bf02406d32771c789983249fa045112f52 |
|
MD5 | 837751bb2d3e5ebfcd2e7960e7b1aa30 |
|
BLAKE2b-256 | d614961d739b6062342b1b5cd2573d3798903f588bb89bee52dc8d60d31271d9 |