private string ConvertString(string sFrom, string TypeOfConversion) { string FromText = txtFrom.Text; string ToText = string.Empty; byte[] oFromBytes = null; //byte[] oToBytes = null; //byte[] ToBytes; System.Text.Encoding oUtf8Encoding = System.Text.Encoding.UTF8; System.Text.Encoding oAsciiEncoding = System.Text.Encoding.ASCII; string sError = string.Empty; switch (TypeOfConversion) { case UrlEncode: ToText = System.Web.HttpUtility.UrlEncode(FromText); break; case UrlDecode: ToText = System.Web.HttpUtility.UrlDecode(FromText); break; case UrlEncodeUnicode: ToText = System.Web.HttpUtility.UrlEncode(FromText); //ToText = System.Web.HttpUtility.UrlEncodeUnicode(FromText); break; case HtmlEncode: try { ToText = System.Web.HttpUtility.HtmlEncode(FromText); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case HtmlDecode: try { ToText = System.Web.HttpUtility.HtmlDecode(FromText); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case Base64Encode: try { oFromBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(FromText); ToText = System.Convert.ToBase64String(oFromBytes); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case Base64Decode: try { oFromBytes = System.Convert.FromBase64String(FromText); ToText = System.Text.ASCIIEncoding.ASCII.GetString(oFromBytes); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; //case Utf8Encode: // try // { // oFromBytes = System.Text.Encoding.Default.GetBytes(FromText); // ToText = System.Text.Encoding.UTF8.GetString(oFromBytes); // ToText += "\r\n"; // Console.WriteLine("Encoded bytes:"); // foreach (Byte b in oFromBytes) // { // ToText += string.Format("[{0}]", b); // } // } // catch (Exception ex) // { // MessageBox.Show(ex.ToString(), "Error"); // } // break; //case Utf8Decode: // try // { // oFromBytes = oUtf8Encoding.GetBytes(FromText); // ToText = oUtf8Encoding.GetString(oFromBytes); // //oFromBytes = oUtf8Encoding.GetBytes(FromText); // //oToBytes = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("iso-8859-1"), oFromBytes); // //ToText = System.Text.Encoding.UTF8.GetString(oToBytes); // } // catch (Exception ex) // { // MessageBox.Show(ex.ToString(), "Error"); // } // break; case XmlEncode: try { XmlDocument oXmlDocument = new XmlDocument(); XmlElement oXmlElement = oXmlDocument.CreateElement("SomeElement"); oXmlElement.InnerText = FromText; ToText = oXmlElement.InnerXml; oXmlElement = null; oXmlDocument = null; } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; //case XmlEscape: // try // { // ToText = System.Security.SecurityElement.Escape(FromText); // } // catch (Exception ex) // { // MessageBox.Show(ex.ToString(), "Error"); // } // break; //case QuotedPrintableDecode: // // http://stackoverflow.com/questions/2226554/c-class-for-decoding-quoted-printable-encoding // try // { // //string sWork = FromText; // //var occurences = new Regex(@"=[0-9A-Z]{2}", RegexOptions.Multiline); // //var matches = occurences.Matches(sWork); // //foreach (Match match in matches) // //{ // // char hexChar= (char) Convert.ToInt32(match.Groups[0].Value.Substring(1), 16); // // sWork = sWork.Replace(match.Groups[0].Value, hexChar.ToString()); // //} // //ToText = sWork.Replace("=\r\n", ""); // ToText = DecodeQuotedPrintables(FromText, "iso-8859-1"); // } // catch (Exception ex) // { // MessageBox.Show(ex.ToString(), "Error"); // } // break; case XmlDecode: try { //ToText = XmlConvert.DecodeName(FromText); XmlDocument oXmlDocument = new XmlDocument(); XmlElement oXmlElement = oXmlDocument.CreateElement("SomeElement"); oXmlElement.InnerXml = FromText; ToText = oXmlElement.InnerText; oXmlElement = null; oXmlDocument = null; } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlBeautify: try { ToText = ""; XmlDocument oXmlDocument = new XmlDocument(); oXmlDocument.InnerXml = FromText; try { StringBuilder sb = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings { Indent = true, IndentChars = " ", NewLineChars = "\r\n", NewLineHandling = NewLineHandling.Replace, CheckCharacters = false }; using (XmlWriter writer = XmlWriter.Create(sb, settings)) { oXmlDocument.Save(writer); ToText = sb.ToString(); oXmlDocument = null; settings = null; } } catch (System.Xml.XmlException XmlEx) { MessageBox.Show(XmlEx.ToString(), "Error"); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlBase64ToHex: try { oFromBytes = System.Convert.FromBase64String(FromText); // Convert to Bytes first. ToText = StringHelper.HexStringFromByteArray(oFromBytes, false); // Now convert it to a hex string. } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlBase64ToHexSpaceDelimited: try { oFromBytes = System.Convert.FromBase64String(FromText); // Convert to Bytes first. ToText = StringHelper.HexStringFromByteArray(oFromBytes, true); // Now convert it to a hex string. } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlTextToHex: try { System.Text.ASCIIEncoding oEncoding = new System.Text.ASCIIEncoding(); oFromBytes = oEncoding.GetBytes(sFrom); ToText = StringHelper.HexStringFromByteArray(oFromBytes, false); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlTextToHexSpaceDelimited: try { System.Text.ASCIIEncoding oEncoding = new System.Text.ASCIIEncoding(); oFromBytes = oEncoding.GetBytes(sFrom); ToText = StringHelper.HexStringFromByteArray(oFromBytes, true); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlHexToCleanHexText: try { if (StringHelper.CleanHexString(FromText, ref ToText, ref sError) == false) { ToText = sError; } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlHexToText: try { if (StringHelper.RoughHexStringToByteArray(FromText, ref oFromBytes, ref sError) == true) { ToText = System.Text.Encoding.ASCII.GetString(oFromBytes); } else { ToText = sError; } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlHexToBase64: try { if (StringHelper.RoughHexStringToByteArray(FromText, ref oFromBytes, ref sError) == true) { //oFromBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(FromText); // First Turn hex string into an byte array. ToText = System.Convert.ToBase64String(oFromBytes); // Now convert to a Base64 string. } else { ToText = sError; } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlHexDumpText: try { ToText = StringHelper.DumpString(FromText); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlBase64ToHexDump: try { oFromBytes = System.Convert.FromBase64String(FromText); // Convert to Bytes first. ToText = StringHelper.HexDumpFromByteArray(oFromBytes); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlConvertVerifyXmlChars: try { ToText = XmlConvert.VerifyXmlChars(FromText); if (ToText != null) { MessageBox.Show("Xml characters verfied."); } } catch (XmlException XmlExx) { MessageBox.Show(XmlExx.ToString(), "Error"); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case UnicodeStringToHexString: try { ToText = StringHelper.UnicodeStringToHexString(FromText); } catch (Exception Ex) { MessageBox.Show(Ex.ToString(), "Error"); } break; case HexStringToUnicodeString: try { ToText = StringHelper.HexStringToUnicodeString(FromText); } catch (Exception Ex) { MessageBox.Show(Ex.ToString(), "Error"); } break; case StringStatistics: try { ToText = StringHelper.GetStringStats(FromText); } catch (XmlException XmlExx) { MessageBox.Show(XmlExx.ToString(), "Error"); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case CheckForNonASCIICharacters: try { ToText = StringHelper.CheckResponseForOddCharacters(FromText); } catch (Exception exCheckForInvalidCharacters) { MessageBox.Show(exCheckForInvalidCharacters.ToString(), "Error"); } break; case sRemoveControlCodes: try { ToText = StringHelper.RemoveControlCodes(FromText); } catch (Exception exRemoveControlCodes) { MessageBox.Show(exRemoveControlCodes.ToString(), "Error"); } break; case sRemoveNonAsciiAndControlCharacters: try { ToText = StringHelper.RemoveNonAsciiAndControlCharacters(FromText); } catch (Exception exRemoveNonAsciiAndControlCharacters) { MessageBox.Show(exRemoveNonAsciiAndControlCharacters.ToString(), "Error"); } break; case sRemoveNonExtendedAsciiAndControlCharacters: try { ToText = StringHelper.RemoveNonExtendedAsciiAndControlCharacters(FromText); } catch (Exception exRemoveNonExtendedAsciiAndControlCharacters) { MessageBox.Show(exRemoveNonExtendedAsciiAndControlCharacters.ToString(), "Error"); } break; //case Utf8DecodeToAscii: // oUtf8Encoding = System.Text.Encoding.UTF8; // oFromBytes = oUtf8Encoding.GetBytes(FromText); // ToBytes = Encoding.Convert(Encoding.UTF8, Encoding.UTF8, oFromBytes); // ToText = System.Text.Encoding.ASCII.GetString(ToBytes); // break; //case Utf32DecodeToAscii: // oUtf8Encoding = System.Text.Encoding.UTF8; // oFromBytes = oUtf8Encoding.GetBytes(FromText); // ToBytes = Encoding.Convert(Encoding.UTF32, Encoding.UTF32, oFromBytes); // ToText = System.Text.Encoding.ASCII.GetString(ToBytes); // break; } return(ToText); }
private string ConvertString(string sFrom, string TypeOfConversion) { string FromText = txtFrom.Text; string ToText = string.Empty; byte[] oFromBytes = null; //byte[] ToBytes; System.Text.Encoding oUtf8Encoding = System.Text.Encoding.UTF8; System.Text.Encoding oAsciiEncoding = System.Text.Encoding.ASCII; string sError = string.Empty; switch (TypeOfConversion) { case UrlEncode: ToText = System.Web.HttpUtility.UrlEncode(FromText); break; case UrlDecode: ToText = System.Web.HttpUtility.UrlDecode(FromText); break; case UrlEncodeUnicode: ToText = System.Web.HttpUtility.UrlEncodeUnicode(FromText); break; case HtmlEncode: try { ToText = System.Web.HttpUtility.HtmlEncode(FromText); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case HtmlDecode: try { ToText = System.Web.HttpUtility.HtmlDecode(FromText); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case Base64Encode: try { oFromBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(FromText); ToText = System.Convert.ToBase64String(oFromBytes); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case Base64Decode: try { oFromBytes = System.Convert.FromBase64String(FromText); ToText = System.Text.ASCIIEncoding.ASCII.GetString(oFromBytes); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; //case Utf8Encode: // try // { // oFromBytes = System.Text.Encoding.Default.GetBytes(FromText); // ToText = System.Text.Encoding.UTF8.GetString(oFromBytes); // } // catch (Exception ex) // { // MessageBox.Show(ex.ToString(), "Error"); // } // break; //case Utf8Decode: // try // { // oFromBytes = oUtf8Encoding.GetBytes(FromText); // ToBytes = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("iso-8859-1"), oFromBytes); // ToText = System.Text.Encoding.UTF8.GetString(ToBytes); // } // catch (Exception ex) // { // MessageBox.Show(ex.ToString(), "Error"); // } // break; //case XmlEncode: // try // { // XmlDocument oXmlDocument = new XmlDocument(); // XmlElement oXmlElement = oXmlDocument.CreateElement("SomeElement"); // oXmlElement.InnerText = FromText; // ToText = oXmlElement.InnerXml; // oXmlElement = null; // oXmlDocument = null; // } // catch (Exception ex) // { // MessageBox.Show(ex.ToString(), "Error"); // } // break; case XmlBase64ToHex: try { oFromBytes = System.Convert.FromBase64String(FromText); // Convert to Bytes first. ToText = StringHelper.HexStringFromByteArray(oFromBytes, false); // Now convert it to a hex string. } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlBase64ToHexSpaceDelimited: try { oFromBytes = System.Convert.FromBase64String(FromText); // Convert to Bytes first. ToText = StringHelper.HexStringFromByteArray(oFromBytes, true); // Now convert it to a hex string. } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlTextToHex: try { System.Text.ASCIIEncoding oEncoding = new System.Text.ASCIIEncoding(); oFromBytes = oEncoding.GetBytes(sFrom); ToText = StringHelper.HexStringFromByteArray(oFromBytes, false); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlTextToHexSpaceDelimited: try { System.Text.ASCIIEncoding oEncoding = new System.Text.ASCIIEncoding(); oFromBytes = oEncoding.GetBytes(sFrom); ToText = StringHelper.HexStringFromByteArray(oFromBytes, true); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlHexToCleanHexText: try { if (StringHelper.CleanHexString(FromText, ref ToText, ref sError) == false) { ToText = sError; } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlHexToText: try { if (StringHelper.RoughHexStringToByteArray(FromText, ref oFromBytes, ref sError) == true) { ToText = System.Text.Encoding.ASCII.GetString(oFromBytes); } else { ToText = sError; } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlHexToBase64: try { if (StringHelper.RoughHexStringToByteArray(FromText, ref oFromBytes, ref sError) == true) { //oFromBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(FromText); // First Turn hex string into an byte array. ToText = System.Convert.ToBase64String(oFromBytes); // Now convert to a Base64 string. } else { ToText = sError; } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlHexDumpText: try { ToText = StringHelper.DumpString(FromText); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlEncode: try { XmlDocument oXmlDocument = new XmlDocument(); XmlElement oXmlElement = oXmlDocument.CreateElement("SomeElement"); oXmlElement.InnerText = FromText; ToText = oXmlElement.InnerXml; oXmlElement = null; oXmlDocument = null; } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlDecode: try { //ToText = XmlConvert.DecodeName(FromText); XmlDocument oXmlDocument = new XmlDocument(); XmlElement oXmlElement = oXmlDocument.CreateElement("SomeElement"); oXmlElement.InnerXml = FromText; ToText = oXmlElement.InnerText; oXmlElement = null; oXmlDocument = null; } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlBeautify: try { ToText = ""; XmlDocument oXmlDocument = new XmlDocument(); oXmlDocument.InnerXml = FromText; try { StringBuilder sb = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings { Indent = true, IndentChars = " ", NewLineChars = "\r\n", NewLineHandling = NewLineHandling.Replace, CheckCharacters = false }; using (XmlWriter writer = XmlWriter.Create(sb, settings)) { oXmlDocument.Save(writer); ToText = sb.ToString(); oXmlDocument = null; settings = null; } } catch (System.Xml.XmlException XmlEx) { MessageBox.Show(XmlEx.ToString(), "Error"); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlBase64ToHexDump: try { oFromBytes = System.Convert.FromBase64String(FromText); // Convert to Bytes first. ToText = StringHelper.HexDumpFromByteArray(oFromBytes); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case XmlConvertVerifyXmlChars: try { ToText = XmlConvert.VerifyXmlChars(FromText); if (ToText != null) { MessageBox.Show("Xml characters verfied."); } } catch (XmlException XmlExx) { MessageBox.Show(XmlExx.ToString(), "Error"); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; //case Utf8DecodeToAscii: // oUtf8Encoding = System.Text.Encoding.UTF8; // oFromBytes = oUtf8Encoding.GetBytes(FromText); // ToBytes = Encoding.Convert(Encoding.UTF8, Encoding.UTF8, oFromBytes); // ToText = System.Text.Encoding.ASCII.GetString(ToBytes); // break; //case Utf32DecodeToAscii: // oUtf8Encoding = System.Text.Encoding.UTF8; // oFromBytes = oUtf8Encoding.GetBytes(FromText); // ToBytes = Encoding.Convert(Encoding.UTF32, Encoding.UTF32, oFromBytes); // ToText = System.Text.Encoding.ASCII.GetString(ToBytes); // break; case StringStatistics: try { ToText = StringHelper.GetStringStats(FromText); } catch (XmlException XmlExx) { MessageBox.Show(XmlExx.ToString(), "Error"); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } break; case CheckForNonASCIICharacters: try { ToText = StringHelper.CheckResponseForOddCharacters(FromText); } catch (Exception exCheckForInvalidCharacters) { MessageBox.Show(exCheckForInvalidCharacters.ToString(), "Error"); } break; case sRemoveControlCodes: try { ToText = StringHelper.RemoveControlCodes(FromText); } catch (Exception exRemoveControlCodes) { MessageBox.Show(exRemoveControlCodes.ToString(), "Error"); } break; case sRemoveNonAsciiAndControlCharacters: try { ToText = StringHelper.RemoveNonAsciiAndControlCharacters(FromText); } catch (Exception exRemoveNonAsciiAndControlCharacters) { MessageBox.Show(exRemoveNonAsciiAndControlCharacters.ToString(), "Error"); } break; case sRemoveNonExtendedAsciiAndControlCharacters: try { ToText = StringHelper.RemoveNonExtendedAsciiAndControlCharacters(FromText); } catch (Exception exRemoveNonExtendedAsciiAndControlCharacters) { MessageBox.Show(exRemoveNonExtendedAsciiAndControlCharacters.ToString(), "Error"); } break; } return(ToText); }