Powered by GeSHi

Source code for asc.cs

(view source code of asc.cs as plain text)

  1. using System;
  2.  
  3. namespace RobvanderWoude
  4. {
  5. 	class Asc
  6. 	{
  7. 		static int Main( string[] args )
  8. 		{
  9. 			if ( args.Length != 1 )
  10. 			{
  11. 				return WriteError( string.Empty );
  12. 			}
  13. 			if ( args[0].Length != 1 )
  14. 			{
  15. 				return WriteError( string.Empty );
  16. 			}
  17. 			try
  18. 			{
  19. 				int result = Convert.ToInt32( Convert.ToByte( Convert.ToChar( args[0].Substring( 0, 1 ) ) ) );
  20. 				Console.WriteLine( "{0}", result );
  21. 				return result;
  22. 			}
  23. 			catch ( Exception e )
  24. 			{
  25. 				return WriteError( e.Message );
  26. 			}
  27. 		}
  28.  
  29. 		// Code to display help and optional error message, by Bas van der Woude
  30. 		public static int WriteError( Exception e )
  31. 		{
  32. 			return WriteError( e == null ? null : e.Message );
  33. 		}
  34.  
  35. 		public static int WriteError( string errorMessage )
  36. 		{
  37. 			/*
  38. 			Asc,  Version 1.00
  39. 			Return the decimal character code for the specified ASCII character
  40.  
  41. 			Usage:  ASC  character
  42.  
  43. 			Where:  character  is an ASCII character
  44.  
  45. 			Note:   The result will be displayed on screen and returned as exit
  46. 			        code ("errorlevel"); in case of errors, exit code will be 0.
  47.  
  48. 			Written by Rob van der Woude
  49. 			http://www.robvanderwoude.com
  50. 			*/
  51.  
  52. 			if ( string.IsNullOrEmpty( errorMessage ) == false )
  53. 			{
  54. 				Console.Error.WriteLine( );
  55. 				Console.ForegroundColor = ConsoleColor.Red;
  56. 				Console.Error.Write( "ERROR: " );
  57. 				Console.ForegroundColor = ConsoleColor.White;
  58. 				Console.Error.WriteLine( errorMessage );
  59. 				Console.ResetColor( );
  60. 			}
  61.  
  62. 			Console.Error.WriteLine( );
  63. 			Console.Error.WriteLine( "Asc,  Version 1.00" );
  64. 			Console.Error.WriteLine( "Return the decimal character code for the specified ASCII character" );
  65. 			Console.Error.WriteLine( );
  66. 			Console.Error.Write( "Usage:  " );
  67. 			Console.ForegroundColor = ConsoleColor.White;
  68. 			Console.Error.WriteLine( "ASC  character" );
  69. 			Console.ResetColor( );
  70. 			Console.Error.WriteLine( );
  71. 			Console.Error.Write( "Where:  " );
  72. 			Console.ForegroundColor = ConsoleColor.White;
  73. 			Console.Error.Write( "character" );
  74. 			Console.ResetColor( );
  75. 			Console.Error.WriteLine( "  is an ASCII character" );
  76. 			Console.Error.WriteLine( );
  77. 			Console.Error.WriteLine( "Note:   The result will be displayed on screen and returned as exit" );
  78. 			Console.Error.WriteLine( "        code (\"errorlevel\"); in case of errors, exit code will be 0." );
  79. 			Console.Error.WriteLine( );
  80. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  81. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  82. 			return 0;
  83. 		}
  84. 	}
  85. }
  86.