using System; using System.Text.RegularExpressions; using System.Web; using System.Windows.Forms; namespace RobvanderWoude { static class HTMLEntities { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.SetHighDpiMode( HighDpiMode.SystemAware ); Application.EnableVisualStyles( ); Application.SetCompatibleTextRenderingDefault( false ); Application.Run( new FormHTMLEntities( ) ); } } public partial class FormHTMLEntities : Form { private void ConvertUnicode() { if ( !string.IsNullOrWhiteSpace( this.textBoxUnicode.Text ) ) { int entity = Convert.ToInt32( (char)this.textBoxUnicode.Text[0] ); this.textBoxHTMLEntity.Text = entity.ToString( ); this.textBoxURLEncoded.Text = HttpUtility.UrlEncode( this.textBoxUnicode.Text ); MoveCursorToEnd( ); } } private void MoveCursorToEnd() { this.textBoxHTMLEntity.SelectionStart = this.textBoxHTMLEntity.Text.Length; this.textBoxHTMLEntity.SelectionLength = 0; this.textBoxUnicode.SelectionStart = this.textBoxUnicode.Text.Length; this.textBoxUnicode.SelectionLength = 0; this.textBoxURLEncoded.SelectionStart = this.textBoxURLEncoded.Text.Length; this.textBoxURLEncoded.SelectionLength = 0; } private void ValidateHTMLEntity() { string text = this.textBoxHTMLEntity.Text; Regex regex = new Regex( "[^\\d]" ); if ( regex.IsMatch( text ) ) { int cursorpos = this.textBoxHTMLEntity.SelectionStart + this.textBoxHTMLEntity.SelectionLength; text = regex.Replace( text, "" ); this.textBoxHTMLEntity.SelectionStart = Math.Min( cursorpos, text.Length ); this.textBoxHTMLEntity.SelectionLength = 0; } else if ( text.Length > 1 ) { int entity = Convert.ToInt32( text ); this.textBoxUnicode.Text = System.Net.WebUtility.HtmlDecode( string.Format( "&#{0};", text ) ); this.textBoxURLEncoded.Text = HttpUtility.UrlEncode( this.textBoxUnicode.Text ); } } private void ValidatURLEncoded() { string text = this.textBoxURLEncoded.Text; Regex regex = new Regex( "[^%\\dA-Fa-f]" ); if ( regex.IsMatch( text ) ) { int cursorpos = this.textBoxURLEncoded.SelectionStart + this.textBoxURLEncoded.SelectionLength; text = regex.Replace( text, "" ); this.textBoxURLEncoded.SelectionStart = Math.Min( cursorpos, text.Length ); this.textBoxURLEncoded.SelectionLength = 0; } else if ( text.Length > 2 ) { regex = new Regex( "^(%[\\dA-Fa-f]{2}){1,2}$" ); if ( regex.IsMatch( text ) ) { char newchar = HttpUtility.UrlDecode( text )[0]; this.textBoxUnicode.Text = newchar.ToString( ); int entity = Convert.ToInt32( newchar ); this.textBoxHTMLEntity.Text = entity.ToString( ); } else { this.textBoxUnicode.Text = string.Empty; this.textBoxHTMLEntity.Text = string.Empty; } } } } }