Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Collections;
  8.  
  9. namespace Telkom1
  10. {
  11. class Hamming
  12. {
  13. private byte[] encodedBytes = new byte[16];
  14. private byte[] AC = new byte[8];
  15.  
  16. private readonly byte[,] H = new byte[8, 16] {
  17. {1,1,1,0,0,1,1,1,1,0,0,0,0,0,0,0},
  18. {0,1,1,1,1,0,1,1,0,1,0,0,0,0,0,0},
  19. {1,0,0,1,0,1,0,1,0,0,1,0,0,0,0,0},
  20. {1,1,1,0,1,0,0,1,0,0,0,1,0,0,0,0},
  21. {0,1,0,1,0,1,1,0,0,0,0,0,1,0,0,0},
  22. {1,0,1,0,1,0,1,0,0,0,0,0,0,1,0,0},
  23. {1,1,0,0,1,1,0,0,0,0,0,0,0,0,1,0},
  24. {1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1}};
  25.  
  26.  
  27. private string ASCII2bin(byte c)
  28. {
  29. string buffer = Convert.ToString(c, 2).PadLeft(8, '0');
  30.  
  31. return buffer;
  32. }
  33.  
  34. private void LoadToEncodedBytes(string bin)
  35. {
  36. byte[] bytes = new byte[bin.Length];
  37.  
  38. int i = 0;
  39.  
  40. foreach (var c in bin)
  41. {
  42. if (c == '0') encodedBytes[i] = 0;
  43. else if (c == '1') encodedBytes[i] = 1;
  44. i++;
  45. }
  46. }
  47.  
  48. public string Encode(string input)
  49. {
  50. string hammingEncoded = "";
  51.  
  52. input = input.Replace(" ", "");
  53. input = input.Replace("\n", "");
  54.  
  55. //Zamiana inputu na pojedyncze znaki
  56. char[] chars = input.ToCharArray();
  57.  
  58. //Zamiana znakow na bity i zaladowanie do encodedBytes
  59. for (int i = 0; i < chars.Length; i++)
  60. {
  61. LoadToEncodedBytes(ASCII2bin((byte)chars[i]));
  62.  
  63. for (int j = 0; j < 8; j++)
  64. {
  65. for (int k = 0; k < encodedBytes.Length; k++)
  66. {
  67. AC[j] += Convert.ToByte(encodedBytes[k] * H[j, k]);
  68. //AC[j] += encodedBytes[k] * H[j, k];
  69. }
  70. AC[j] %= 2;
  71. }
  72.  
  73. for (int m = 8; m < encodedBytes.Length; m++)
  74. {
  75. encodedBytes[m] = AC[m - 8];
  76. }
  77.  
  78. for (int j = 0; j < encodedBytes.Length; j++)
  79. {
  80. if (j == 8) hammingEncoded += " ";
  81.  
  82. hammingEncoded += encodedBytes[j];
  83. }
  84. hammingEncoded += "\n";
  85. }
  86. return hammingEncoded;
  87. }
  88.  
  89. /*public void Decode()
  90. {
  91. for (int i = 0; i < 8; i++)
  92. {
  93. for (int j = 0; j < 16; j++)
  94. {
  95. decoded[i] += bytes[j] * H[i, j];
  96. }
  97. decoded[i] %= 2;
  98. }
  99. Console.WriteLine(decoded);*/
  100. }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement