C # character encoding decoding Encoder and decoder

Source: Internet
Author: User

In network transport and file operations, if the amount of data is large and needs to be divided into smaller, fast, it may appear that at the end of a block of data there is a mismatched high surrogate, and its matching low surrogate is in the next chunk.

This time using the encoding GetBytes method to deal with the more troublesome, we directly use the encoder processing.

Encoder can encode a set of characters into a sequence of bytes. Instead, decoder can decode the encoded sequence of bytes into a sequence of characters.

To obtain an implementation of an instance of the Encoder class, the application should use the Getencoder method Encoding implementation.

getbytecount method determines how many bytes result in encoding a set of Unicode characters, and The getbytes method performs the actual encoding. " The GetByteCount method determines how many bytes are encoded by a set of Unicode characters and the GetBytes method performs the actual encoding. encoder class. " > There are several versions of these two methods that have the encoder class. encoding. GetBytes. " > For more information, see Encoding.GetBytes.

encoder object maintains state information between successive calls to GetBytes or convert methods so the it can correctly encode character sequences that span blocks. " > a encoder object maintains state information to successive calls to the GetBytes or Convert method so that it can correctly encode the sequence of characters across the block. encoder also preserves trailing characters at the end of data blocks and uses the trailing characters in th E Next encoding operation. " > encoder also retains trailing character data blocks at the end and uses trailing characters in the next encoding operation.

For example, the end of a data block may not match the high surrogate, and a matching low surrogate may be in the next block of data. therefore, Getdecoder and Getencoder are useful in network transport and file operations, because these operations typically process data instead of full dataflow blocks.

To obtain an implementation of an instance of the Decoder class, the application should use the Getdecoder method Encoding implementation.

The GetCharCount method determines how many characters cause a sequence of bytes to be decoded and the GetChars method performs the actual decoding.There are Decoder classes in several versions of these two methods . For more information, see Encoding.getchars.

Onedecoder objects maintain state information to successive calls to the GetChars or Convert method, so it can correctly decode a byte sequence that spans a block. decoder also preserves trailing bytes at the end of data blocks and uses the trailing bytes in the next Dec Oding operation. " > decoder also retains trailing bytes at the end of the data block and is used in the next decoding operation. getdecoder and getencoder are Useful for network transmission and file operations because those operations often deal with blocks of data instead of a C Omplete data stream. " >

therefore, Getdecoder and Getencoder are very useful for network transport and file operations, because these operations typically process data instead of full dataflow blocks.

1         stringstr ="in the lower Sakamoto, there are Riddermark @@";2Char[] chars =Str. ToCharArray ();3Debug.Log ("string="+New string(chars));4         //obtaining encoder instances;5Encoder Encoder =Encoding.UTF8.GetEncoder ();6         //computes the length of the byte array required for character serialization;7         byte[] bytes =New byte[Encoder. GetByteCount (chars,0, chars. Length,true)];8         //convert to byte sequence by GetBytes;9Encoder. GetBytes (chars,0, chars. Length, Bytes,0,true);Ten Debug.Log (bitconverter.tostring (bytes)); OneDebug.Log ("encoding.utf8.getstring="+Encoding.UTF8.GetString (bytes)); A         //obtaining decoder instances; -Decoder Decoder =Encoding.UTF8.GetDecoder (); -         //computes the length of the character array corresponding to the byte array; the         intCharsize = decoder. GetCharCount (Bytes,0, Bytes. Length); -char[] CHS =New Char[charsize]; -         //for character conversion; -         intCharlength = decoder. GetChars (Bytes,0, bytes. Length, CHS,0); +Debug.Log ("Decoder Bytes to String ="+New string(CHS));

Output

MSDN Encoder >>> https://msdn.microsoft.com/zh-cn/library/system.text.encoder%28v=vs.110%29.aspx

MSDN Decoder >>> https://msdn.microsoft.com/zh-cn/library/system.text.decoder%28v=vs.110%29.aspx

C # character encoding decoding Encoder and decoder

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.