/// <summary> /// Check the Byte and Hex Allignment for missmatch. If so it corrects them and returns the result. /// </summary> /// <param name="_byte">The Byte Allignment as enum.</param> /// <param name="_hex">The Hex Allignment as enum.</param> /// <returns>The corrected Allignment if missmatch, else it returns the orig values.</returns> public int[] CheckByteAndHexAllign(ByteAllign _byte, HexAllign _hex) { int[] result = new int[2]; result[0] = (int)_byte; result[1] = (int)_hex; if (_hex == HexAllign.x4) { if (_byte == ByteAllign.b16 || _byte == ByteAllign.b8) { result[0] = (int)ByteAllign.b4; } } else if (_hex == HexAllign.x8) { if (_byte == ByteAllign.b16) { result[0] = (int)ByteAllign.b8; } } return(result); }
/// <summary> /// Hexify a sring array. /// </summary> /// <param name="source">The source string array to use.</param> /// <param name="_byte">The Byte Allignment.</param> /// <param name="_hex">The Hex Allignment.</param> /// <returns>The Hexifyed string array.</returns> public string[] Hexify(string[] source, ByteAllign _byte, HexAllign _hex) { int[] corrected = CheckByteAndHexAllign(_byte, _hex); // Check if Byte and Hex are correct alligned. _byte = (ByteAllign)corrected[0]; _hex = (HexAllign)corrected[1]; List <string> hexifyed = new List <string>(); // Initialize a new string List to store our hexifyed strings. string toHex = string.Empty; // The string which shall be hexifyed. string x0 = "0x"; // Represents the '0x' Hex value descriptor. string patt = " "; // Pattern. int toHexCount = 0; // Count for the string to hexify. int subStringCount = (int)_byte * 2; // Set the length of the substring acording to byte length. foreach (string line in source) // Loop over all lines now. { if (!string.IsNullOrEmpty(line)) // If string hase some data. { int hexCount, byteCount; // Byte and hex counters. hexCount = byteCount = 0; // Init to 0. toHex = ReplaceLineBreak(line.Replace(" ", "")); // Get string to hexify and delete white space and line breaks out of it. toHexCount = toHex.Length; // Set the length of the string to hexify as a counter to decrement. string hex = string.Empty; // Represents the hexifyed values. for (int i = 0; i < toHex.Length; i += subStringCount) // Loop over all Characters and increment the counter with the length of the substring. { if (toHexCount < subStringCount) { subStringCount = toHexCount; // If the length of the string is shorter then the substring we reset the substring length. } byteCount += (int)_byte; // Count byte counter up based on the defined byte length. toHexCount -= subStringCount; // Decrement the Counter for the string to hexify with the length of the substring. hexCount++; // Increment the hex counter. if (toHexCount == 0 || byteCount == 16) // If we have reached the end of the string or already collected 16 bytes. { hex += x0 + toHex.Substring(i, subStringCount); // Generate the hexifyed string without patting on end. hexifyed.Add(hex); // Add the new hexifyed string to the string list. if (toHexCount == 0) { break; // If we have reached the end break the loop now to avoid additional pattern on end. } if (byteCount == 16) // IF we haved collected 16 bytes. { byteCount = hexCount = 0; // Reset the counters. hex = string.Empty; // Empty the new hex string. } } else { hex += x0 + toHex.Substring(i, subStringCount) + patt; // else Generate the hexifyed string with additional pattern on end. } if (hexCount == (int)_hex) // If hex counter matches defined hex length. { hex += patt; // Add additional pattern. hexCount = 0; // Clear counter. } } } } return(hexifyed.ToArray()); }