C # serialization and Transcoding

Source: Internet
Author: User

First, we will write twoCode

 

1. byte [] q = system. Text. encoding.Utf8. Getbytes ("ha ");

2.
Memorystream stream = new memorystream ();
Binaryformatter formatter = new binaryformatter ();
Formatter. serialize (stream, "ha ");
Stream. Close ();
Byte [] QQ = stream. toarray ();

The first method is to convert the "ha" stringUtf8The second is serialization"

The difference is that

I.

1 is simple Transcoding

2. serialize the string object "ha". An object includes the right content (attributes, methods, indexers, and so on)

The serialization object is to convert the object to the byte [] format. Because the object members are complex and of different types, it is not as simple as transcoding.

II.

We will find that 2 does not specify the encoding format. Do you not need to specify the encoding format for serialization? Of course, this is impossible. In my experiment, when serializing an object, to convert the string of the data member in the object to byte [], followUtf8Code. I have no experiments for other types.

 

Why?Utf8InsteadUnicode?

Let's take a look at the following explanations:

Utf8Efficiency
1. Each English letter or number occupies 1 byte;
2. Letters of the pan-European and Slavic languages account for 2 bytes;
3. Chinese characters occupy 3 bytes.
If Unicode is used, the English format is changed to 2 bytes.
It turns out that laomei lost Chinese efficiency to take care of herself.

It can be seen that utf8 is a very attractive solution for English, but it is not very cost-effective for Chinese. It uses only 2 bytes for both ANSI and Unicode/ucs2 encoding, however, utf8 requires 3 bytes.
The following are some statistics showing the average bytes required to store each character of a file using utf8:
1. The average Latin system is 1.1 bytes;
2. An average of 1.7 bytes for Greek, Russian, Arabic, and Hebrew;
3. About 3 bytes for most other texts, such as Chinese, Japanese, Korean, and Hindi;
4. More than 4 bytes of characters are rarely used.

 

At the beginning, when I used serialization, I wondered why I didn't need to specify encoding. Instead, I didn't specify encoding, but the serialization object was already ready.Algorithm, (It is possible that many different encodings are used for different types. The customization is complicated, meaningless, and the optimal encoding specified by the system) it does not provide an interface for external users to specify encoding (you can customize serialization, but you do not know whether you can customize serialization encoding)

I am not sure it is correct.

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.