【问题标题】:(7,4) Hamming code using strings(7,4) 使用字符串的汉明码
【发布时间】:2014-04-11 04:13:22
【问题描述】:

我正在尝试使用 (7,4) 汉明码对字符串进行编码和解码。我开始只用位来做,我认为它正在工作。但是,我不确定如何在这种情况下涉及字符串。我的一位朋友告诉我使用地图词典,但我不知道该怎么做。

有人可以帮我解决这个问题吗?

这是我到目前为止所做的:

import java.util.*;
class Hamming
 {
public static void main(String arg[])
 {
Scanner sc = new Scanner(System.in);

int dataCode[] = new int[7];
int parity[] = new int[4];
int comCode[] = new int[11];
int receive[] = new int[11];
int parityR[] = new int[4];
int receiveD[] = new int[7];
int syndrome[] = new int[4];
int check;

System.out.println("Enter the data code (7-bits): ");
for(int i=0;i<7;i++)
  dataCode[i]=sc.nextInt();


parity[0] = dataCode[0]^dataCode[1]^dataCode[3]^dataCode[4]^dataCode[6];
parity[1] = dataCode[0]^dataCode[2]^dataCode[3]^dataCode[5]^dataCode[6];
parity[2] = dataCode[1]^dataCode[2]^dataCode[3];
parity[3] = dataCode[4]^dataCode[5]^dataCode[6];


System.out.print("\nCode Word is ");

comCode[0] = parity[0];  
comCode[1] = parity[1];  
comCode[2] = dataCode[0];
comCode[3] = parity[2];  
comCode[4] = dataCode[1];  
comCode[5] = dataCode[2];
comCode[6] = dataCode[3];  
comCode[7] = parity[3];  
comCode[8] = dataCode[4];
comCode[9] = dataCode[5];  
comCode[10] = dataCode[6];

System.out.println();
for(int i=0; i<11;i++)
  System.out.print(comCode[i]+ " ");

System.out.println("\n\nEnter codeword which you received: ");
for(int i=0;i<11;i++)
  receive[i] = sc.nextInt();

parityR[0] = receive[0];
parityR[1] = receive[1];
receiveD[0] = receive[2];
parityR[2] = receive[3];
receiveD[1] = receive[4];
receiveD[2] = receive[5];
receiveD[3] = receive[6];
parityR[3] = receive[7];
receiveD[4] = receive[8];
receiveD[5] = receive[9];
receiveD[6] = receive[10];


syndrome[0] = parityR[0] ^ receiveD[0] ^ receiveD[1] ^ receiveD[3] ^ receiveD[4] ^ receiveD[6];
syndrome[1] = parityR[1] ^ receiveD[0] ^ receiveD[2] ^ receiveD[3] ^ receiveD[5] ^ receiveD[6];
syndrome[2] = parityR[2] ^ receiveD[1] ^ receiveD[2] ^ receiveD[3];
syndrome[3] = parityR[3] ^ receiveD[4] ^ receiveD[5] ^ receiveD[6];

check = (syndrome[0]*1) + (syndrome[1]*2) + (syndrome[2]*4) + (syndrome[3]*8);

System.out.print("\nResult: ");
if(check == 0)
  System.out.println("\nNo error");
else
{
  System.out.println("\nError is at " + check);
  if(receive[check - 1] == 0)
    receive[check - 1] = 1;
  else
    receive[check - 1]=0;
}

System.out.println("Code word after Correction: ");
for(int i=0;i<11;i++)
  System.out.print(receive[i]+" ");

}
}

有人可以告诉我应该如何开始做或查看它吗? 提前致谢

【问题讨论】:

    标签: java hamming-code


    【解决方案1】:

    您的代码可能没有实现通常被认为是 (7,4) 汉明码。您使用 7 个输入位并将它们映射到 11 传输位,而 (7,4) 汉明码执行 4 到 7 映射。也许你想看看 http://en.wikipedia.org/wiki/Hamming%287,4%29

    一旦您将映射更改为一次转换 4 位 以下:

    使用 byte[] b = s.getBytes("UTF-8") 将字符串 s 转换为字节。

    将它们转换为二进制表示:

    for (j = 0; j < 8; j++) {
      nextbit = b[i] & 0x01;
      b[i] = b[i] >> 1
    }
    

    然后您将代码两次应用于每个转换的字节,即上部和下部。 (或者你可以转换成半字节直接操作,见Extracting Nibbles from Java Bytes

    总结:转换字符串 -> 字节 -> 半字节,应用正确的 (7,4) 汉明码

    要让您的字符串恢复解码您传输的单词,请将它们转换回字节数组b1 并通过s = new String(b1, "UTF-8") 获取字符串

    【讨论】:

    • 您能否向我解释一下如何一次转换 4 位?我会使用这个:codework[] = {0x00, 0x1E, 0x2D, 0x33, 0x4B, 0x55, 0x66, 0x78, 0x87, 0x99, 0xAA, 0xB4, 0xCC, 0xD2, 0xE1, 0xFF};但我到底应该怎么做呢?如果我从文件中得到一个字符串,我应该怎么做?将其转换为十六进制?
    • 使用真正的 7,4 Hemmung 代码,这个一次使用 4 位,而不是您的代码使用的 7。
    猜你喜欢
    • 1970-01-01
    • 2013-07-28
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多