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.3-pp37-pypy37_pp73-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 43aceff56282b2b82b52088bc557070738b548f6896555b87322a0d64ff9d591 |
|
MD5 | d71974acd9c43eb372a3d8b003964f1b |
|
BLAKE2b-256 | 7a17197b1d0d955ea43a50f3185d9317e9f15fb648e45f90970519c0b5016ca8 |
Hashes for hamming_codec-0.3.3-pp37-pypy37_pp73-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 798ab4ee90f2e9cbf277da468e66c4ec77d9366d1db6bded93e5a9d78a106c4f |
|
MD5 | d34760f1c2349a84eb5e1cd757132ff4 |
|
BLAKE2b-256 | 7099a9b7e2fb4400e55538a3fe8e64e690e002a053ed746840feef94ed96a08a |
Hashes for hamming_codec-0.3.3-pp37-pypy37_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 474d614418acbe81f492672f3c88306ba72f852be664ca353628945c72bfeb26 |
|
MD5 | 7e44f49dd682d10043e7b1283c2afbb0 |
|
BLAKE2b-256 | 80e06ad939ca1d4beb71eec062c37a991935582f778e3d7ac92b0a0877bc080d |
Hashes for hamming_codec-0.3.3-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5123b4508e1f0127df644e40ab92e1c3ba003a824e1713a7de5d85f2dbd303ed |
|
MD5 | d9773aa3a01e5300e884f8a536431251 |
|
BLAKE2b-256 | d17ec052415f3dc8ba664326f0e2c99030c5a67703700f814a12f3b204be4714 |
Hashes for hamming_codec-0.3.3-cp310-cp310-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b56aaf776add437e2c8db3f5e353cb9173bb3c119f5881775326804512c158df |
|
MD5 | 7f4353462116678096382bcdd121e53e |
|
BLAKE2b-256 | 0f2b2ad6b9332a4275d83f6a725bee5a9ee71092deb6059d5b558db0e7a602d1 |
Hashes for hamming_codec-0.3.3-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f33f2314b79c447a79833e0780c5f7d5c01e24ea220048a106a2f9fcfb57c81 |
|
MD5 | 2209b6b3101be65ca50124a46db0c4c5 |
|
BLAKE2b-256 | 12dcf74b241ac7d023ecb05fcde2db130f233c94f95059c9fb6424869e6c4cfc |
Hashes for hamming_codec-0.3.3-cp310-cp310-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8022d1915c43f989775b16737d83696b8f6a7d1b4446acf717bc4d5e97d88383 |
|
MD5 | dfef07c256dccecf33a0ad25ae06ca4d |
|
BLAKE2b-256 | 03595b70b0c6c1d21529e6480ee7b0346f2f5467db74c0c0350ff3f821137f50 |
Hashes for hamming_codec-0.3.3-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3ce64424e8fb2f0be8baf6574b3d15c17be1cb095ad594ac03a1d5044e37fe5 |
|
MD5 | b7887e38f8e6138e8e11341d4681d097 |
|
BLAKE2b-256 | 91ae7af426cb4bc1772d5336b198476aa1f2eee506f807a80b7c1c543a457072 |
Hashes for hamming_codec-0.3.3-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb6ddca2df5035cf921e5969bffad5d5333d2c7f01b10640826665a7aa90f288 |
|
MD5 | 53b3e9fb2ce7372362474053997b2b50 |
|
BLAKE2b-256 | 36fe2800b5de714e0e2a75bf2385882d152b578461e354e163202f5f55e59792 |
Hashes for hamming_codec-0.3.3-cp39-cp39-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5fc5d7a7a07dc24e219eb89ba80de7ac3bbfc39a79551d2da753384dbed6b4ee |
|
MD5 | 3b234c672a2493e809ca09bac934e6e5 |
|
BLAKE2b-256 | 37ca8d7eeca53524bc21dfc89b6742c9cc6a6c55c483913e5455574be1ab6988 |
Hashes for hamming_codec-0.3.3-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf27ccd48717176e7b4312354703567788355d062cb31425ecf0f76c7f3724eb |
|
MD5 | b36e22d2297219d3bf522cda9ffbb59e |
|
BLAKE2b-256 | 53e7bb099e898e00cf5a908381f2d69e1e6869f31482a37e58b75e2f50955b27 |
Hashes for hamming_codec-0.3.3-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dce34dd4084c1cf9e2f06b948ea3746459bc31604663d90d4e887d6514f0a802 |
|
MD5 | c6872899925a3b1f15b533633c396625 |
|
BLAKE2b-256 | fdba98c9b089e742fff9a5f6e119bfa2ac9a527f3debc91b49e88eeb17abb484 |
Hashes for hamming_codec-0.3.3-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d740caecef3f8586020c77404618b29b1b87bc8b290255fd0da7c2837c7812c |
|
MD5 | 5744801e44886165ae667bf834c55a37 |
|
BLAKE2b-256 | 44c0865d29048ba54d416a9dac2b252b25bf86e9b3cb12a7ac44f2b93207a8d5 |
Hashes for hamming_codec-0.3.3-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa41fa0051578debb5208190d9b9bf70b0c6e5722de4473ae317e458bcf45031 |
|
MD5 | 8ef48a78e3ed4901c5a97faf4e3e60f0 |
|
BLAKE2b-256 | 8be1c499360b42e473302ce9c9eed90e9cc236a3517461ae3640351b99e001a5 |
Hashes for hamming_codec-0.3.3-cp38-cp38-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40c50267d0cabbd8afc847127c706094675264d11bbc5e9d0412e2fadcccee47 |
|
MD5 | 5fa097b70dd636ed22803b65e0c5e631 |
|
BLAKE2b-256 | 940cacc69390b4fb1cc6f7cad1acc61cb2112a53145206423aabcff8e0f3208a |
Hashes for hamming_codec-0.3.3-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 911df2b40c8ad0c0a0038bbe358fd95b1edc03d9cfeb06377c530b8c12cd3ab5 |
|
MD5 | b4e39a5fece9601f0ad182febc34c542 |
|
BLAKE2b-256 | 57ca9c3caeb32b7741f3732289a5528dcf20fc0363aff85c7b86e6d3fafeb178 |
Hashes for hamming_codec-0.3.3-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a22ba5832c235ae3f579ba5720c4029cd790ec291224cdd9d74208364c07fd5b |
|
MD5 | f1b9c8ef06e2578aa2514d91fd2e6f60 |
|
BLAKE2b-256 | 01d41203722601008349cac4f95883fb8c37f66124a647979ce67abf0e368228 |
Hashes for hamming_codec-0.3.3-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 475316a96fdb04a9432f5e7880fab7e79669e3a9ef4120f349d3be4f4423cff9 |
|
MD5 | bd35ca139fe1f673965d1792f12de4e2 |
|
BLAKE2b-256 | 5881ae29d2ba06c03b42c88bccb4e640c0857c4726487693d0b00c5165a9f8b7 |
Hashes for hamming_codec-0.3.3-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 879ef4364172d3a26013a6e32e8d8a88d620c71253ecb1865e3bd412d044f164 |
|
MD5 | 23131657e7656be246877bd49ed86f15 |
|
BLAKE2b-256 | e07f0804f8458886a23f1c2c6e141cda4a595880891c61e38b453d9c41347740 |
Hashes for hamming_codec-0.3.3-cp37-cp37m-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b12136ee2ab4d1060a1a210f737ad94537b33d00f492fe685af6247aa8cde8e |
|
MD5 | b8056f98ba96fcab373828d50cf1f0fd |
|
BLAKE2b-256 | 0888e8afa51724fbdfb7e27a5da52a303ec979d3bb1f5fa0f6a0fd82cca80cfd |
Hashes for hamming_codec-0.3.3-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62f91c96e64cb3f8041e8f5015e54fe6baeae7b309f6b73f289390f12936b4e5 |
|
MD5 | cd9285de7df0e1c5a8efbca8a2013b37 |
|
BLAKE2b-256 | eb7b8c8eebeeb85643146aeff72b8fabf92371b9695e42a7ce5aa97c07bc460e |
Hashes for hamming_codec-0.3.3-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f6498b0af6d0a16e5bb1ad55adc34aff6a01b153d64439b9ab43912cd0f4935 |
|
MD5 | d10b226edeefd2e93e2eb19619fe4f6a |
|
BLAKE2b-256 | fbe8c37bf6222c45820be02df98b5cfd194ff4997e6648725f08c79a58bd6880 |
Hashes for hamming_codec-0.3.3-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ef1fdd92500b7337bbe8400af2751fd737af9d37ddaea634a03420a57f22804 |
|
MD5 | 1d158e5ab91e96cea90164f95a7f2e7c |
|
BLAKE2b-256 | 2788625c2febfd23bd26a907aa19ad0d36c57bc619b87721caba4af7ffa9ded9 |
Hashes for hamming_codec-0.3.3-cp36-cp36m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2c301b97621eeb7a7154d2cc0a186082a3259d84788eb872b16b9b81490c7bc |
|
MD5 | ada6502a928f16c5bd6ebf6cd8ec5ee8 |
|
BLAKE2b-256 | 1de555d79a0798a25c8231af289fa737466e27f09232de0f5395db4ac823802f |
Hashes for hamming_codec-0.3.3-cp36-cp36m-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5bd7ffcd2523dd65be350a2cc95476165f6360f3a25a48884f3c0d228bf4919 |
|
MD5 | 5f1861b7b18776be0fa36838600b11f8 |
|
BLAKE2b-256 | 83d6de1c4ad69dd89d58de47ea515c31457e45d39cde6cf3117ffbd1da7526f3 |
Hashes for hamming_codec-0.3.3-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b0f130ebc4faa2d8ee0185af63c8c99d5f94b42a770d3ce175eab4856d0c5a8 |
|
MD5 | 1aa62fc6b9faf8e0a0c1d3cda59a44f7 |
|
BLAKE2b-256 | d7f7f1aa5c5acaedacd35c7e143577d46b60c7767be743998ddfbc8ea48d4946 |
Hashes for hamming_codec-0.3.3-cp36-cp36m-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e525ce964cf4dd0d18b23edf0a3af5cbce80905b9782c46082016d5284c178a |
|
MD5 | add5e82dae808bee8041f30c56401761 |
|
BLAKE2b-256 | 3e1a58a12f75d36862e7a0ac720643c0c355709409bd30693b6267198c0979b8 |
Hashes for hamming_codec-0.3.3-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d87beeca6f51acd5b941d72925188dfc636a4fff4c4116b8810c4a08f16a2e9 |
|
MD5 | 807fa52623e7dbb6729f50c76fbb8d91 |
|
BLAKE2b-256 | 5919bc072e41603226e8f4f507ce53e95d92db79fb47356bda1f5fb80014931e |