[Note] note that BinaryWriter writes a string to a small pitfall. The length prefix "length-prefixed" and "cbinarywriter" will be added before the string.

Source: Internet
Author: User

[Note] note that BinaryWriter writes a string to a small pitfall. The length prefix "length-prefixed" and "cbinarywriter" will be added before the string.

Previously, I thought that BinaryWriter would write the string to the binary string strictly according to the encoding specified during the construction (without the BOM UTF8 parameter). The following code:

// Write the string "a" into the stream and get the byte group datausing (var MS = new MemoryStream () {using (var bw = new BinaryWriter (MS) of the stream )) {bw. write ("a");} byte [] data = ms. toArray ();}

Because the UTF-8 encoding of the letter a is 97, I expect that data has only one element and the value is 97. In fact, data has two elements: 1, 97, obviously 97 represents a, but the first 1 is a ghost. If you try another string, one or more bytes will be added to the front, and the value will be floating. In shortBw does not write the binary string in the original source, but adds some materials,This may cause problems in scenarios where the bytes are strictly required to be correct, such as the http Request body. The server will force these extra bytes. I searched and found that MSDN and stackoverflow mentioned earlier that the extra bytes actually indicate the length of the string, which is called the length prefix (length-prefixed). According to a Q & A of SO, this is for the ReadString method of BinaryReader. It knows the length before it knows where to read it. Therefore, if the stream reader is not BinaryReader, these length prefixes are redundant or even harmful. In this case, BinaryWriter cannot be used. write (string) method, to Write a clean string binary, you can do this:

Bw. Write (Encoding. UTF8.GetBytes ("a"); // select the correct Encoding as needed

That is, the byte Group of the string is first encoded, and then written to the byte group using BinaryWriter. Write (byte.

-Wen Bi-

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.