Key code:
usingSystem.Text;namespaceyanzhiwei.dotnet2.utilities.common{// <summary> // ASCII helper class // </summary> Public Static classAsciihelper {/** Knowledge:* ASCII is a set of computer coding systems based on the Latin alphabet. It is mainly used to display modern English and other Western European languages. It is now the most versatile single-byte encoding system and is equivalent to ISO/IEC 646. * In the computer, all data is stored and computed using a binary number representation (because the computer represents 1 and 0, respectively, with high and low levels), such as* A, B, C, d such as 52 letters (including uppercase), and 0, 1 and other numbers and some commonly used symbols (such as *, #, @, etc.) are stored in the computer also to use* binary number to represent, and specifically which binary numbers to indicate which symbol, of course everyone can agree their own set (this is called code), and if you want to* Communicate with each other without causing confusion, then everyone must use the same coding rules, so the United States standardization organizations have introduced the ASCII code, unified set up* Describes what binary numbers are used to denote common symbols. * * ASCII code uses the specified 7-bit or 8-bit binary number combination to represent 128 or 256 possible characters. The standard ASCII code is also called the base ASCII code, using 7-bit two* binary number to represent all uppercase and lowercase letters, numbers 0 through 9, punctuation, and special control characters used in American English. */ #regionConvert to ASCII code// <summary> // Convert to ASCII code // </summary> /// <param name= "Data" > Characters </param> /// <returns>ascii yards </returns> Public Static intToascii ( This CharData) {ASCIIEncoding _asciiencoding =NewASCIIEncoding ();Char[] _array =New Char[1] {data};return(int) _asciiencoding.getbytes (_array) [0]; }// <summary> /// Convert the string to an ASCII code // </summary> /// <param name= "Data" > String </param> // <returns>byte[]</returns> Public Static byte[] Toascii ( This stringData) {byte[] Asciibytes =NULL;if(!string. IsNullOrEmpty (data)) Asciibytes = Encoding.ASCII.GetBytes (data);returnAsciibytes; }#endregion #regionConvert ASCII code to characters// <summary> /// convert ASCII code to character // </summary> /// <param name= "Asciicode" >scii yards </param> /// <returns> characters </returns> Public Static CharToChar ( This byteAsciicode) {ASCIIEncoding _asciiencoding =NewASCIIEncoding ();byte[] _array =New byte[] {asciicode};return_asciiencoding.getstring (_array) [0]; }#endregion}}
Unit tests:
usingMicrosoft.VisualStudio.TestTools.UnitTesting;namespaceyanzhiwei.dotnet2.utilities.common.tests{[TestClass ()] Public classasciihelpertests {[TestMethod ()] Public voidToasciitest () {int_actual = Asciihelper.toascii ('. '); Assert.AreEqual (_actual);byte[] _actualarray = Asciihelper.toascii ("Hello"); Collectionassert.areequal (New byte[5] {101, 108, 108, 111}, _actualarray); } [TestMethod ()] Public voidTochartest () {Char_actual = Asciihelper.tochar (46); Assert.AreEqual ('. ', _actual); } }}
[C#]asciihelper