0

I am trying to use a (7,4) hamming code to encode and decode a string. I started to do it with bits only and I think it's working. However, I'm not sure how to involve strings in the situation. One of my friends told me to use map dictionary but I'm not sure how to do so.

Can someone help me with this?

Here is what I've done so far:

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]+" ");

}
}

can someone please tell me how should I start doing it or looking at it? Thanks in advance

rullzing
  • 622
  • 2
  • 10
  • 22

1 Answers1

0

Your code probably does not implement what is usually considered a (7,4) hamming code. You use 7 input bits and map them to 11 transmission bits, while the (7,4) hamming code does a 4 to 7 mapping. Maybe you want to have a look at http://en.wikipedia.org/wiki/Hamming%287,4%29

Once you have changed your mapping to convert 4 bits at a time do the following:

Convert your string s to bytes using byte[] b = s.getBytes("UTF-8").

Convert them into binary representation:

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

Then you apply your code twice to each converted byte, namely to the upper and lower part. (Or you could convert into halfbytes and operate directly on them, see Extracting Nibbles from Java Bytes)

Summary: Convert String -> bytes -> halfbytes, apply correct (7,4) hamming code

To get your string back decode your transmitted words, convert them back to a byte array b1 and get the String by s = new String(b1, "UTF-8")

Community
  • 1
  • 1
Drunix
  • 3,313
  • 8
  • 28
  • 50
  • Could you please explain to me how would I convert 4 bits at a time? I would use this: codework[] = {0x00, 0x1E, 0x2D, 0x33, 0x4B, 0x55, 0x66, 0x78, 0x87, 0x99, 0xAA, 0xB4, 0xCC, 0xD2, 0xE1, 0xFF}; but what exactly should I do with it? If I got a string from a file, what exactly should I do? convert it to hex? – rullzing Apr 11 '14 at 20:57
  • Use the real 7,4 Hemmung code, this one used 4 bits at a time unstead of the 7 your code used. – Drunix Apr 12 '14 at 05:12