C # simple ASCII Code Conversion

Source: Internet
Author: User

Convert letters to numbers

Byte [] array = new byte [1]; // defines an array Array
Array = system. Text. encoding. ASCII. getbytes (string); // String Conversion letter
Int asciicode = (short) (array [0]);
ASCII code = convert. tostring (asciicode); // convert the ASCII code to the string type

Convert numbers to letters
Byte [] array = new byte [1];
Array [0] = (byte) (convert. toint32 (ASCII Code); // the ASCII code is forcibly converted to binary
Converted letters = convert. tostring (system. Text. encoding. ASCII. getstring (array ));

 

 

 

In the encoding process, indexes of some numbers are often used to convert to letters. For example, the number of columns of cells in Excel is represented by uppercase letters, to convert a number to a letter, use the getstring method in the asciiencoding class of C. See the following example:

///
/// Convert numbers to letters
///
/// Number to be converted to a letter (the number range is [])
///
Private string nuntochar (INT number)
{
If (65 <= Number & 90> = number)
{
System. Text. asciiencoding = new system. Text. asciiencoding ();
Byte [] btnumber = new byte [] {(byte) number };
Return asciiencoding. getstring (btnumber );
}
Return "the number is not in the conversion range ";
}

///
/// Convert 1, 2, 3,..., 35, 36 to A, B, C,..., Y, Z
///
/// Number to be converted to a letter (the number range is [1, 36])
///
Private string nunbertochar (INT number)
{
If (1 <= Number & 36> = number)
{
Int num = Number + 64;
System. Text. asciiencoding = new system. Text. asciiencoding ();
Byte [] btnumber = new byte [] {(byte) num };
Return asciiencoding. getstring (btnumber );
}
Return "the number is not in the conversion range ";
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.