C#返回指定ASCII码的十进制字符的代码

580 阅读1分钟

将写代码过程常用的内容做个收藏,下面代码内容是关于C#返回指定ASCII码的十进制字符的代码,应该能对小伙伴有些帮助。 using System;

namespace RobvanderWoude { class Asc { static int Main( string[] args ) { if ( args.Length != 1 ) { return WriteError( string.Empty ); } if ( args[0].Length != 1 ) { return WriteError( string.Empty ); } try { int result = Convert.ToInt32( Convert.ToByte( Convert.ToChar( args[0].Substring( 0, 1 ) ) ) ); Console.WriteLine( "{0}", result ); return result; } catch ( Exception e ) { return WriteError( e.Message ); } }

	public static int WriteError( Exception e )
	{
		return WriteError( e == null ? null : e.Message );
	}

	public static int WriteError( string errorMessage )
	{
		Asc,  Version 1.00
		Return the decimal character code for the specified ASCII character

		Usage:  ASC  character

		Where:  character  is an ASCII character

		Note:   The result will be displayed on screen and returned as exit
		        code ("errorlevel"); in case of errors, exit code will be 0.

		Written by Rob van der Woude

		if ( string.IsNullOrEmpty( errorMessage ) == false )
		{
			Console.Error.WriteLine( );
			Console.ForegroundColor = ConsoleColor.Red;
			Console.Error.Write( "ERROR: " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.WriteLine( errorMessage );
			Console.ResetColor( );
		}

		Console.Error.WriteLine( );
		Console.Error.WriteLine( "Asc,  Version 1.00" );
		Console.Error.WriteLine( "Return the decimal character code for the specified ASCII character" );
		Console.Error.WriteLine( );
		Console.Error.Write( "Usage:  " );
		Console.ForegroundColor = ConsoleColor.White;
		Console.Error.WriteLine( "ASC  character" );
		Console.ResetColor( );
		Console.Error.WriteLine( );
		Console.Error.Write( "Where:  " );
		Console.ForegroundColor = ConsoleColor.White;
		Console.Error.Write( "character" );
		Console.ResetColor( );
		Console.Error.WriteLine( "  is an ASCII character" );
		Console.Error.WriteLine( );
		Console.Error.WriteLine( "Note:   The result will be displayed on screen and returned as exit" );
		Console.Error.WriteLine( "        code ("errorlevel"); in case of errors, exit code will be 0." );
		Console.Error.WriteLine( );
		Console.Error.WriteLine( "Written by Rob van der Woude" );
		return 0;
	}
}

}