簡體   English   中英

(7,4)使用字符串的漢明碼

[英](7,4) Hamming code using strings

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

}
}

有人可以告訴我如何開始做或看看嗎? 提前致謝

您的代碼可能未實現通常視為(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
}

然后,將代碼兩次應用於每個轉換后的字節,即上下部分。 (或者您可以轉換成半字節並直接對其進行操作,請參閱從Java字節中提取字節

摘要:轉換字符串->字節->半字節,應用正確的(7,4)漢明碼

為了使您的字符串恢復解碼的傳輸的單詞,將它們轉換回字節數組b1並通過s = new String(b1, "UTF-8")獲得字符串

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM