On coding encoding__ Coding in C #

Source: Internet
Author: User

1. This description is often seen when getting started in C #:. NET string is Unicode encoding.
After getting started and not looking too much at this basic book and having more contact with some coding problems, my subconscious mind always feels that string has many encodings, utf8,unicode,ascii, and so on, and doesn't think C # has gb2312 encoding.
2.system.text.encoding.default seems to solve all the coding, because every time I use default.getstring () to read the string in the stream succeeds. So default should be based on byte encoding, for example, if your byte sequence is ASCII encoding, then default is ASCII encoding.
Only a few days ago in the Java community in CSDN to see a coding problem, plus a few hours of their own experiments, finally to the coding problem to clarify the clues.
. NET string is really only Unicode type. So the byte sequence encoded in the format is converted to a string and is eventually represented in Unicode.
System.Text.Encoding.Default is the encoding of the current ANSI code page of the system (copied on MSDN), that is, the current system's encoding. (In our machine is generally "gb2312") This is my every time with the default read file stream is correct, and must read with the default is the correct reason----actually use encoding.getencoding ("GB2312") is the same.
At each byte[]-->string (Other-->unicode) and string-->byte[]

(Unicode-->other), there will be coded conversions.

For example, the usual conversion has a place to encode, such as StreamReader (string path [, System.Text.Encoding Encoding]), Response.Charset, which is equivalent to telling the system byte[] What is the code, this time. NET is decoded using the encoding you specify, and then converted to a string of Unicode encoding. That is, whenever we specify a code that simply refers to byte[].
That is, Encoding.UTF8.GetString (byte[] buffer) is the code that tells the system that the buffer is UTF8.
byte[] buf = Encoding.UTF8.GetBytes (String str) is the BUF encoding that tells the system to return is UTF8.

You might tell the system a fake coding way, or you have not told the system and byte[] encoding is not the default encoding, then the system decoding will still be done in the specified encoding, in the machine it appears that he still decoded successfully, and then converted to Unicode encoding, because the machine only know 0, 1-byte sequence, He did not know whether the decoded things were chaotic and everything seemed to him to be normal.

But we go to see, we will find that the string has become some inexplicable symbols without any meaning, this is called garbled. Is like: "You Yi ge", originally is spell English "have a", you have to tell others this is English, others use English to spell, do not know what is going on, became garbled, hehe.
That is, the byte sequence is encoded in the specified encoding, it has a specific encoding, but it is inherently neutral and does not contain any encoded information.

The encoding method is theoretically independent of language, but it actually needs language to support it. such as "GBK" (gb2312 Extended) encoding in Java, but not in. Net. You use Encoding.GetEncoding ("GBK") to throw an exception.

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.