static string GoUpwards(int[,] matrix, string data, int size, int x, int y, int mask) { while (y >= 0) { for (int j = 0; j < 2; j++) { x -= j; if (x < 0) { break; } if (matrix[y, x] == 1 || matrix[y, x] == -1) { continue; // Все служебные белые клетки должны стать -1 } if (data.Length == 0) { matrix[y, x] = 0; continue; } if (data[0] == '1') { matrix[y, x] = 1; } data = data.Remove(0, 1); if (Dicts.Mask(mask, x, y) == 0) { if (matrix[y, x] == 0) { matrix[y, x] = 1; } else { matrix[y, x] = 0; } } // Убрать коментарии, чтобы вернуть всё на место //matrix[y, x] = 1; //DisplayMatrix(matrix); //Thread.Sleep(15); } y -= 1; x += 1; } if (x == 5) { x -= 1; } return(data); }
static string GoDownwards(int[,] matrix, string data, int size, int x, int y, int mask) { y = 0; while (y < size) { for (int j = 0; j < 2; j++) { x -= j; if (x < 0) { break; } if (matrix[y, x] == 1 || matrix[y, x] == -1) { continue; } if (data.Length == 0) { matrix[y, x] = 0; continue; } if (data[0] == '1') { matrix[y, x] = 1; } data = data.Remove(0, 1); if (Dicts.Mask(mask, x, y) == 0) { if (matrix[y, x] == 0) { matrix[y, x] = 1; } else { matrix[y, x] = 0; } } //matrix[y, x] = 1; //DisplayMatrix(matrix); //Thread.Sleep(15); } y += 1; x += 1; } return(data); }
public static string EncodeAlphaNumeric(string input, int version, int correctionLevel) { string encodedLine = string.Empty; for (int i = 0; i < input.Length - 1; i += 2) { var fElement = Dicts.AlphanumericDictionary(input[i]); var sElement = Dicts.AlphanumericDictionary(input[i + 1]); var sum = fElement * 45 + sElement; encodedLine += $"{Convert.ToString(sum, 2).PadLeft(11, '0')} "; } if (input.Length % 2 == 1) { int tmp = Dicts.AlphanumericDictionary(input[input.Length - 1]); encodedLine += $"{Convert.ToString(tmp, 2).PadLeft(6, '0')} "; } Console.WriteLine($"INPUT BITS: {encodedLine}"); switch (version) { case int _ when version <= 9: encodedLine = encodedLine.Insert(0, $"{Convert.ToString(input.Length, 2).PadLeft(9, '0')} "); break; case int _ when version <= 26: encodedLine = encodedLine.Insert(0, $"{Convert.ToString(input.Length, 2).PadLeft(11, '0')} "); break; case int _ when version <= 40: encodedLine = encodedLine.Insert(0, $"{Convert.ToString(input.Length, 2).PadLeft(13, '0')} "); break; } encodedLine = encodedLine.Insert(0, "0010 "); encodedLine = AddEndOfLine(encodedLine, version, correctionLevel); encodedLine = DivideLine(encodedLine); encodedLine = AddCodewords(encodedLine, version, correctionLevel); encodedLine = DivideBlocks(encodedLine, version, correctionLevel); encodedLine = CreateCorrectionByte(encodedLine, version, correctionLevel); encodedLine = ShuffleCodewords(encodedLine); return(encodedLine); }
public static string EncodeECI(string input, int version, int correctionLevel) { string encodedLine = string.Empty; string encodedData = string.Empty; string ECIAssignmentNumber = input.Substring(0, 7).Remove(0, 1); input = input.Remove(0, 7); var ECIAssignmentNumberBinary = Convert.ToString(int.Parse(ECIAssignmentNumber), 2).PadLeft(8, '0'); encodedLine = encodedLine.Insert(0, ECIAssignmentNumberBinary); encodedLine = encodedLine.Insert(0, "0111 "); byte[] bytes = System.Text.Encoding.GetEncoding(Dicts.ECI(ECIAssignmentNumber)).GetBytes(input); for (int i = 0; i < bytes.Length; i++) { var tmp = bytes[i]; encodedData += $"{Convert.ToString(tmp, 2).PadLeft(8, '0')} "; } Console.WriteLine($"INPUT BITS: {encodedData}"); switch (version) { case int _ when version <= 9: encodedData = encodedData.Insert(0, $"{Convert.ToString(bytes.Length, 2).PadLeft(8, '0')} "); break; case int _ when version <= 40: encodedData = encodedData.Insert(0, $"{Convert.ToString(bytes.Length, 2).PadLeft(16, '0')} "); break; } encodedData = encodedData.Insert(0, "0100 "); encodedLine += $" {encodedData}"; encodedLine = encodedLine.TrimEnd(); encodedLine = AddEndOfLine(encodedLine, version, correctionLevel); encodedLine = DivideLine(encodedLine); encodedLine = AddCodewords(encodedLine, version, correctionLevel); Console.WriteLine($"+ Codewords {encodedLine}"); encodedLine = DivideBlocks(encodedLine, version, correctionLevel); encodedLine = CreateCorrectionByte(encodedLine, version, correctionLevel); encodedLine = ShuffleCodewords(encodedLine); return(encodedLine); }
public static string CreateCorrectionByte(string encodedLine, int version, int correctionLevel) { MainClass main = new MainClass(); int[,] corrBytePerBlockArr = new int[4, 40]; corrBytePerBlockArr = main.ReadCorrectionBytePerBlock(); byte[] GaloisField = main.ReadGaloisField(); byte[] BackGaloisField = main.ReadBackGaloisField(); string correctionBytes = string.Empty; int corrBytePerBlock = corrBytePerBlockArr[correctionLevel - 1, version - 1]; byte[] Polynomial = Dicts.PolynomialDict(corrBytePerBlock); string[] blocks = encodedLine.Split(' '); byte[] newArr = default; encodedLine += "|"; for (int i = 0; i < blocks.Length; i++) { correctionBytes = string.Empty; newArr = new byte[Math.Max(corrBytePerBlock, blocks[i].Length / 8)]; byte[] bytes = new byte[blocks[i].Length / 8]; for (int j = 0; j < blocks[i].Length / 8; ++j) { bytes[j] = Convert.ToByte(blocks[i].Substring(8 * j, 8), 2); } for (int j = 0; j < bytes.Length; j++) { newArr[j] = bytes[j]; } for (int j = 0; j < blocks[i].Length / 8; ++j) { byte A = newArr[0]; //newArr = newArr.Where((val, idx) => idx != 0).Append(0).ToArray(); for (int l = 1; l < newArr.Length; l++) { newArr[l - 1] = newArr[l]; newArr[l] = 0; } if (A == 0) { continue; } byte B = BackGaloisField[A]; byte BPolynomial; for (int k = 0; k < corrBytePerBlock; k++) { //BPolynomial[k] = (byte)(Polynomial[k] + B); //newArr[k] = (byte)(GaloisField[B] ^ BPolynomial[k]); BPolynomial = (byte)((Polynomial[k] % 255 + B % 255) % 255); newArr[k] = (byte)(GaloisField[BPolynomial] ^ newArr[k]); } } Console.WriteLine($"Correction Bytes start of block {i+1}"); for (int k = 0; k < corrBytePerBlock; k++) { correctionBytes += $"{Convert.ToString(newArr[k], 2).PadLeft(8, '0')}"; Console.Write($" {Convert.ToString(newArr[k], 2).PadLeft(8, '0')}"); } Console.WriteLine($"\nCorrection Bytes end of block {i+1}"); correctionBytes += " "; encodedLine += correctionBytes; } return(encodedLine); }