アプリ版:「スタンプのみでお礼する」機能のリリースについて

ハミング符号で符号化された 0,1,0,1,1,0,1, 1, 1,0,0, 1,0,1, 0,1,0,1,1, 0,0 ,0 ,0,1,0,1,0,0 のようなビット列を受信した. 8bit の ASCIIコードだとして解読してください。

A 回答 (1件)

C++で書くと、これで解読できると思います。


#include <iostream>
#include <vector>
#include <bitset>

// Parity check equations
bool check_parity(const std::bitset<7>& bits) {
bool p1 = bits ^ bits[3] ^ bits[4] ^ bits[2] ^ bits;
bool p2 = bits ^ bits[1] ^ bits[4] ^ bits[5] ^ bits;
bool p3 = bits[3] ^ bits[1] ^ bits[4];
return (p1 == bits[1]) && (p2 == bits[3]) && (p3 == bits);
}

// Correct error in a 7-bit code
void correct_error(std::bitset<7>& bits) {
int error_bit = 0;
error_bit += bits[1] ? 1 : 0;
error_bit += bits[3] ? 2 : 0;
error_bit += bits ? 4 : 0;
if (error_bit > 0) {
bits.flip(error_bit - 1);
}
}

int main() {
std::vector<std::bitset<7>> codes = {
std::bitset<7>("0101101"),
std::bitset<7>("1110100"),
std::bitset<7>("1001010"),
std::bitset<7>("0101101"),
std::bitset<7>("0000101"),
std::bitset<7>("0101010"),
std::bitset<7>("0000000"),
std::bitset<7>("1010000")
};

std::string message = "";
for (const auto& code : codes) {
if (!check_parity(code)) {
correct_error(code);
}
message += static_cast<char>(code.to_ulong());
}

std::cout << "Decoded message: " << message << std::endl;

return 0;
}
    • good
    • 1

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!