Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for chr.cs

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

  1. using System;
  2.  
  3.  
  4. namespace RobvanderWoude
  5. {
  6. 	class Chr
  7. 	{
  8. 		static string progver = "1.02";
  9.  
  10.  
  11. 		static int Main( string[] args )
  12. 		{
  13. 			if ( args.Length > 1 || ( args.Length ==1 && Console.IsInputRedirected ) )
  14. 			{
  15. 				return ShowHelp( );
  16. 			}
  17. 			try
  18. 			{
  19. 				string input;
  20. 				if ( Console.IsInputRedirected )
  21. 				{
  22. 					input = Console.In.ReadToEnd( );
  23. 				}
  24. 				else
  25. 				{
  26. 					input = args[0];
  27. 				}
  28. 				if ( Convert.ToInt32( input ).ToString( ) != input )
  29. 				{
  30. 					return ShowHelp( );
  31. 				}
  32. 				if ( Convert.ToInt32( input ) > 255 || Convert.ToInt32( input ) < 0 )
  33. 				{
  34. 					return ShowHelp( );
  35. 				}
  36. 				Console.Write( "{0}", Convert.ToChar( Convert.ToByte( Convert.ToInt32( input ) ) ) );
  37. 			}
  38. 			catch ( Exception )
  39. 			{
  40. 				return ShowHelp( );
  41. 			}
  42. 			return 0;
  43. 		}
  44.  
  45.  
  46. 		public static int ShowHelp( )
  47. 		{
  48. 			/*
  49. 			Chr,  Version 1.02
  50. 			Return the ASCII character for the specified decimal character code
  51.  
  52. 			Usage:  CHR  charcode
  53.  
  54. 			   or:  some_program | CHR
  55.  
  56. 			Where:  charcode       decimal number in the range 0..255
  57. 			        some_program   program generating a decimal number in the range 0..255
  58.  
  59. 			Note:   This version of CHR requires .NET framework 4.5.
  60.  
  61. 			Written by Rob van der Woude
  62. 			https://www.robvanderwoude.com
  63. 			*/
  64.  
  65. 			Console.Error.WriteLine( );
  66.  
  67. 			Console.Error.WriteLine( "Chr,  Version {0}", progver );
  68.  
  69. 			Console.Error.WriteLine( "Return the ASCII character for the specified decimal character code" );
  70.  
  71. 			Console.Error.WriteLine( );
  72.  
  73. 			Console.Error.Write( "Usage:  " );
  74. 			Console.ForegroundColor = ConsoleColor.White;
  75. 			Console.Error.WriteLine( "CHR  charcode" );
  76. 			Console.ResetColor( );
  77.  
  78. 			Console.ForegroundColor = ConsoleColor.White;
  79. 			Console.Error.WriteLine( "   or:  " );
  80. 			Console.Error.WriteLine( "some_program | CHR" );
  81. 			Console.ResetColor( );
  82.  
  83. 			Console.ForegroundColor = ConsoleColor.White;
  84. 			Console.Error.Write( "        some_program" );
  85. 			Console.ResetColor( );
  86. 			Console.Error.WriteLine( "   program generating a decimal number in the range 0..255" );
  87.  
  88. 			Console.Error.WriteLine( );
  89.  
  90. 			Console.Error.Write( "Where:  " );
  91. 			Console.ForegroundColor = ConsoleColor.White;
  92. 			Console.Error.Write( "charcode" );
  93. 			Console.ResetColor( );
  94. 			Console.Error.WriteLine( "       decimal number in the range 0..255" );
  95.  
  96. 			Console.Error.WriteLine( );
  97.  
  98. 			Console.Error.WriteLine( "Note:   This version of CHR requires .NET framework 4.5." );
  99.  
  100. 			Console.Error.WriteLine( );
  101.  
  102. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  103. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  104. 			return 1;
  105. 		}
  106. 	}
  107. }
  108.  

page last modified: 2024-02-26; loaded in 0.0189 seconds