A hypothetical study of C # encoding.unicode.getbytes/encoding.unicode.getstring function

Source: Internet
Author: User

Many of us know encoding.unicode this stuff, It is a Unicode byte stream used to get a string that includes converting Unicode bytes to a generic string on. NET, but there are a lot of people who don't know how it's done inside, and I was through MultiByteToWideChar

Implemented with WideCharToMultiByte , but I don't know how to implement it internally.

First we need to understand how the Unicode string is stored in memory, and Unicode specifies that a character takes two bytes and the length of the wchar_t is the same

Why should I mention this because if you don't know how long it's hard for you to write code down unless you CTRL + C && CTRL + V

In memory a string is assumed to be "medium" it corresponds to a short int value of 20013 then in memory it will be stored like this


X

low:0010 1101

HIGH:0100 1110


low:1011 0100

high:0111 0010

The left side is low and the right side is high. A high is a power of low, a byte has 8/bit Binary

form:1111 1111 if calculated as a decimal medium range in 0~255 and 256 times

So what we can get from the above Formula : (Low * 0) + (high * ^ 1)


Example code:

/span>

        private void Form1_Load (object sender, EventArgs e) {byte[] Bfx = getuniocdebytes ("China");            byte[] Bfy = Encoding.Unicode.GetBytes ("China");        String str = getunicodestring (BFX);            } private byte[] Getuniocdebytes (string str) {int size = 0; if (str! = NULL && (size = str).                Length) > 0) {byte[] buffer = new byte[size * 2];                    for (int i = 0; i < size; i++) {char chr = str[i];                    Buffer[i * 2] = (byte) (CHR & 0xff);                Buffer[i * 2 + 1] = (byte) (CHR >> 8);            } return buffer;        } return new Byte[size];            } private String getunicodestring (byte[] buffer) {int size = 0; if (buffer! = NULL && (size = buffer).    Length) >= 2) {size = size% 2;//sizeof (wchar_t)            char[] value = new CHAR[SIZE/2];                for (int i = 0; i < size; I + = 2) VALUE[I/2] = (char) ((Buffer[i + 1] << 8) + buffer[i]);            return new string (value); } return string.        Empty; }

A hypothetical study of C # encoding.unicode.getbytes/encoding.unicode.getstring function

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.