0

我正在尝试使用 (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]+" ");

}
}

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

4

1 回答 1

0

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

将映射更改为一次转换 4 位后,请执行以下操作:

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

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

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

然后将代码两次应用于每个转换的字节,即上部和下部。(或者您可以转换为半字节并直接对其进行操作,请参阅从 Java 字节中提取半字节

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

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

于 2014-04-11T06:14:55.823 回答