(7,4)使用字符串的汉明码

时间:2014-04-11 04:13:23

标签: java hamming-code

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

}
}

有人可以告诉我,我该如何开始这样做或者看一下呢? 提前致谢

1 个答案:

答案 0 :(得分:0)

您的代码可能无法实现通常认为的代码 (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

摘要:转换字符串 - &gt; bytes - &gt; halfbytes,应用正确的(7,4)汉明码

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