Conversion of C # strings, byte arrays, and memory streams-top of the IT wave

Source: Internet
Author: User

Defines the string variable as str, the memory rheology is MS, and the bit array is BT

1. String = = array of bits

(1)byte[] Bt=system.text.encoding.default.getbytes (" string");(2)byte[] bt= Convert.frombase64string (" string");       

Add:

System.Text.Encoding.Unicode.GetBytes (str); System.Text.Encoding.UTF8.GetBytes (str); System.Text.Encoding.GetEncoding ("// Specify encoding method  
string str =  " China? Ss123? "; byte[] bytes = System.Text.Encoding.Default.GetBytes (str); //gb2312 encoded Chinese characters accounted for 2 bytes, English alphabet accounted for 1 bytes bytes length 12string s = System.Text.Encoding.Default.GetString (new byte[] {Bytes[0],bytes[1]}); // decoded to "medium"          
byte[] bytes = {// Result: abcdef ASCII code table 

Common methods:

GetString  is overloaded. When overridden in a derived class, decodes a sequence of bytes into a string. GetChars   is overloaded. When overridden in a derived class, decodes a sequence of bytes into a set of characters. GetBytes   is overloaded. When overridden in a derived class, encodes a set of characters into a sequence of bytes. GetByteCount  is overloaded. When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters. GetCharCount  is overloaded. When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes. Getdecoder When overridden in a derived class, gets a decoder that converts an encoded sequence of bytes into a sequence of characters. Getencoder When overridden in a derived class, gets a decoder that converts a sequence of Unicode characters to an encoded sequence of bytes

2. Bit array = string

(1)string str=system.text.encoding.default.getstring (BT);(2)string str=convert.tobase64string (BT );

3. String = Flow

(1) MemoryStream ms=new MemoryStream (System.Text.Encoding.  Default.getbytes (" string")));(2) MemoryStream ms=new MemoryStream (Convert.frombase64string (" string "));        

4. Stream = = string

(1)string str=convert.tobase64string (Ms. ToArray ());(2)string str=system.text.encoding.default.getstring (Ms. ToArray ());   

5. Array of bits = flow

(1) MemoryStream ms=new MemoryStream (BT);(2) MemoryStream ms=new MemoryStream (); Ms. Read (BT,0,BT. lenght);    

6. Stream = array of bits

(1)byte[] Bt=ms. ToArray ();(2) MemoryStream ms=new MemoryStream (); Ms. Write (BT,0,ms. Length);    

Conversion between C # strings, byte arrays, and memory streams-top of the IT wave

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.