Different encoding or decoding functions must be applied according to different data contents in order to obtain the corresponding byte or string data.

Source: Internet
Author: User

Different encoding or decoding functions must be applied according to different data contents in order to obtain the corresponding byte or string data.

This program example will create four different byte data, and use three different encoding methods to convert the original byte data into a string, observe the result of the encoding function processing, the program code is as follows:

static void Main (string[] args) { byte[] ibyte1, Ibyte2, ibyte4;  byte[] ibyte3 = new byte[1024];     Ibyte1 = System.Text.Encoding.ASCII.GetBytes ("0123456789");  ibyte2 = System.Text.Encoding.ASCII.GetBytes ("Zhang Limin");  randomnumbergenerator.create (). GetBytes (Ibyte3);  ibyte4 = new byte[]{0,1,2,3,4};  show (IBYTE1);  show (IBYTE2);  show (Ibyte3);  show (IBYTE4);  console.readline (); } private static void Show (byte[] ibyte) { console.writeline ("/**********************************************/n ");  console.writeline ("ibyte[0]:" + ibyte[0]. ToString () + "n");  string ascii2str = Encoding.ASCII.GetString (ibyte);  console.writeline ("Ascii2str:" + ascii2str + "T");  console.writeline ("Ascii2str. Length: "+ ascii2str. Length + "n");  string unicode2str = Encoding.Unicode.GetString (ibyte);  console.writeline ("Unicode2str:" + unicode2str + "T");  console.writeline ("Unicode2str. Length: "+ unicode2str. Length + "n");  string utf82str = Encoding.UTF8.GetString (ibyte);  console.writeline ("Utf82str:" + utf82str + "T");  console.writeline ("Utf82str. Length: "+ utf82str. Length + "n");  byte[] Ascii2byte = Encoding.ASCII.GetBytes (ASCII2STR);  byte[] Unicode2byte = Encoding.Unicode.GetBytes (UNICODE2STR);  byte[] Utf82byte = Encoding.UTF8.GetBytes (UTF82STR);  console.writeline (Ascii2byte. Length + "" +    Unicode2byte. Length + "" + utf82byte. Length + "n");  console.writeline ("/**********************************************/n"); After executing the program, it is found that the IBYTE1 variable is encoded in ASCII or UTF8, resulting in the same string content and length as the original variable. The IBYTE2 variable is encoded in ASCII or UTF8, although the resulting string data has the same length as the original variable, but the data content differs from the original variable. After the IBYTE3 variable is ASCII encoded, the resulting string data is the same length as the original variable. After the IBYTE4 variable is encoded in ASCII or UTF8, the resulting string data is the same length as the original variable, but the data content differs from the original variable.  

As you can tell from the above results, before converting a byte to a string, you must first select the appropriate conversion function for the contents of the data before you can get the result that meets the requirements.

Different encoding or decoding functions must be applied according to different data contents in order to obtain the corresponding byte or string data.

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.