Rob van der Woude's Scripting Pages
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 readonly string progver = "1.02";
  8.  
  9.  
  10. 		static int Main( string[] args )
  11. 		{
  12. 			if ( args.Length != 1 || ( args.Length == 1 && args[0] == "/?" ) || ( args.Length == 1 && Console.IsInputRedirected ) )
  13. 			{
  14. 				return ShowHelp( string.Empty );
  15. 			}
  16. 			string input;
  17. 			if ( Console.IsInputRedirected )
  18. 			{
  19. 				input = Console.In.ReadToEnd( ).Substring( 0, 1 );
  20. 			}
  21. 			else
  22. 			{
  23. 				input = args[0].Substring( 0, 1 );
  24. 			}
  25. 			try
  26. 			{
  27. 				int result = Convert.ToInt32( Convert.ToByte( Convert.ToChar( input ) ) );
  28. 				Console.WriteLine( "{0}", result );
  29. 				return result;
  30. 			}
  31. 			catch ( Exception e )
  32. 			{
  33. 				return ShowHelp( e.Message );
  34. 			}
  35. 		}
  36.  
  37.  
  38. 		public static int ShowHelp( string errorMessage = "" )
  39. 		{
  40. 			/*
  41. 			Asc,  Version 1.02
  42. 			Return the decimal character code for the specified ASCII character
  43.  
  44. 			Usage:  ASC  character
  45.  
  46. 			   or:  some_program | ASC
  47.  
  48. 			Where:  character      ASCII character
  49. 			        some_program   program generating an ASCII charachter
  50.  
  51. 			Notes:  Only the first character is processed, any characters
  52. 			        following the first one will be ignored.
  53. 			        The result will be displayed on screen and returned as exit
  54. 			        code ("errorlevel"); in case of errors, exit code will be 0.
  55. 			        This version of ASC requires .NET framework 4.5.
  56.  
  57. 			Written by Rob van der Woude
  58. 			https://www.robvanderwoude.com
  59. 			*/
  60.  
  61. 			if ( !string.IsNullOrWhiteSpace( errorMessage ) )
  62. 			{
  63. 				Console.Error.WriteLine( );
  64. 				Console.ForegroundColor = ConsoleColor.Red;
  65. 				Console.Error.Write( "ERROR: " );
  66. 				Console.ForegroundColor = ConsoleColor.White;
  67. 				Console.Error.WriteLine( errorMessage );
  68. 				Console.ResetColor( );
  69. 			}
  70.  
  71. 			Console.Error.WriteLine( );
  72.  
  73. 			Console.Error.WriteLine( "Asc,  Version {0}", progver );
  74.  
  75. 			Console.Error.WriteLine( "Return the decimal character code for the specified ASCII character" );
  76.  
  77. 			Console.Error.WriteLine( );
  78.  
  79. 			Console.Error.Write( "Usage:  " );
  80. 			Console.ForegroundColor = ConsoleColor.White;
  81. 			Console.Error.WriteLine( "ASC  character" );
  82. 			Console.ResetColor( );
  83.  
  84. 			Console.Error.WriteLine( );
  85.  
  86. 			Console.Error.Write( "   or:  " );
  87. 			Console.ForegroundColor = ConsoleColor.White;
  88. 			Console.Error.WriteLine( "some_program | ASC" );
  89. 			Console.ResetColor( );
  90.  
  91. 			Console.Error.WriteLine( );
  92.  
  93. 			Console.Error.Write( "Where:  " );
  94. 			Console.ForegroundColor = ConsoleColor.White;
  95. 			Console.Error.Write( "character" );
  96. 			Console.ResetColor( );
  97. 			Console.Error.WriteLine( "      ASCII character" );
  98.  
  99. 			Console.ForegroundColor = ConsoleColor.White;
  100. 			Console.Error.Write( "        some_program" );
  101. 			Console.ResetColor( );
  102. 			Console.Error.WriteLine( "   program generating an ASCII charachter" );
  103.  
  104. 			Console.Error.WriteLine( );
  105.  
  106. 			Console.Error.WriteLine( "Notes:  Only the first character is processed, any characters" );
  107.  
  108. 			Console.Error.WriteLine( "        following the first one will be ignored." );
  109.  
  110. 			Console.Error.WriteLine( "        The result will be displayed on screen and returned as exit" );
  111.  
  112. 			Console.Error.WriteLine( "        code (\"errorlevel\"); in case of errors, exit code will be 0." );
  113.  
  114. 			Console.Error.WriteLine( "        This version of ASC requires .NET framework 4.5." );
  115.  
  116. 			Console.Error.WriteLine( );
  117.  
  118. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  119.  
  120. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  121.  
  122. 			return 0;
  123. 		}
  124. 	}
  125. }
  126.  

page last modified: 2024-04-16; loaded in 0.0227 seconds