Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Collections;
- namespace Telkom1
- {
- class Hamming
- {
- private byte[] encodedBytes = new byte[16];
- private byte[] AC = new byte[8];
- private readonly byte[,] H = new byte[8, 16] {
- {1,1,1,0,0,1,1,1,1,0,0,0,0,0,0,0},
- {0,1,1,1,1,0,1,1,0,1,0,0,0,0,0,0},
- {1,0,0,1,0,1,0,1,0,0,1,0,0,0,0,0},
- {1,1,1,0,1,0,0,1,0,0,0,1,0,0,0,0},
- {0,1,0,1,0,1,1,0,0,0,0,0,1,0,0,0},
- {1,0,1,0,1,0,1,0,0,0,0,0,0,1,0,0},
- {1,1,0,0,1,1,0,0,0,0,0,0,0,0,1,0},
- {1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1}};
- private string ASCII2bin(byte c)
- {
- string buffer = Convert.ToString(c, 2).PadLeft(8, '0');
- return buffer;
- }
- private void LoadToEncodedBytes(string bin)
- {
- byte[] bytes = new byte[bin.Length];
- int i = 0;
- foreach (var c in bin)
- {
- if (c == '0') encodedBytes[i] = 0;
- else if (c == '1') encodedBytes[i] = 1;
- i++;
- }
- }
- public string Encode(string input)
- {
- string hammingEncoded = "";
- input = input.Replace(" ", "");
- input = input.Replace("\n", "");
- //Zamiana inputu na pojedyncze znaki
- char[] chars = input.ToCharArray();
- //Zamiana znakow na bity i zaladowanie do encodedBytes
- for (int i = 0; i < chars.Length; i++)
- {
- LoadToEncodedBytes(ASCII2bin((byte)chars[i]));
- for (int j = 0; j < 8; j++)
- {
- for (int k = 0; k < encodedBytes.Length; k++)
- {
- AC[j] += Convert.ToByte(encodedBytes[k] * H[j, k]);
- //AC[j] += encodedBytes[k] * H[j, k];
- }
- AC[j] %= 2;
- }
- for (int m = 8; m < encodedBytes.Length; m++)
- {
- encodedBytes[m] = AC[m - 8];
- }
- for (int j = 0; j < encodedBytes.Length; j++)
- {
- if (j == 8) hammingEncoded += " ";
- hammingEncoded += encodedBytes[j];
- }
- hammingEncoded += "\n";
- }
- return hammingEncoded;
- }
- /*public void Decode()
- {
- for (int i = 0; i < 8; i++)
- {
- for (int j = 0; j < 16; j++)
- {
- decoded[i] += bytes[j] * H[i, j];
- }
- decoded[i] %= 2;
- }
- Console.WriteLine(decoded);*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement