"C #" Explore Data Transfer Objects 1

Source: Internet
Author: User

What is "reading a string from the current stream." The string has a length prefix and is encoded as an integer 7 bits at a time. ”

To explore:

Write a simple program:

FileStream fs= New FileStream ("D:\\qqq.txt", FileMode.Create);
var bw = new BinaryWriter (FS, Encoding.UTF8);//usually can not write code here plus easy to understand
Bw. Write ("Hello world!");
Console.WriteLine ("Write successfully");
Bw. Close ();
Fs. Close ();


FileStream FS2 = new FileStream ("D:\\qqq.txt", FileMode.Open);
var br = new BinaryReader (FS, Encoding.UTF8);
Console.WriteLine (Br. ReadString ());

1. Specify or default encoding when writing to a file so that the character can be converted to a character set corresponding encoding (ASCII octal hex)

The transfer or storage is saved as the corresponding standard transportable binary (from these binary to binary, network transfer binary).

2. When opened with the editor, the default will give us 10 binary, to the character set corresponding to the display text. (The computer is very smart to do things very well, sister like such a boyfriend it)

Ps:

Take a look at the Qqq.txt file you just wrote.

Start with UE view) 16 Binary 0C = 10 binary 12, exactly the character length.

Take a look at BinaryWriter.Write.

Read and write Echo, cooperate with use.

The other overloads are the same usage.

================================== Advanced Research ==================================

byte[] buffer = new BYTE[512];
while (size = NetStream. Read (buffer, 0, buffer. Length)) >0)
{
Fs. Write (buffer, 0, size);
Len + = size;
}

And

var br = new BinaryReader (NetStream);
Console.WriteLine (Br. ReadString ());

The two methods that fetch data from the same stream have different results, why not?

Read will only return byte[] data objects, ReadString can only get binarywriter in the data.

Why does ReadString appear garbled when BinaryWriter does not write to the content?

To test several situations.

In the case of non-write binarywriter, the receiver processing ReadString will read the first part of the data into the readstring, the rest of the data is read.

NetStream will be monitored after transmission starts

After the sender has added a using, there is no need to care about whether the bytes length received by the server is greater than 0, (guess there are two methods of transmission monitoring 1 through whether there is data transmission, 2.close, the level of the dispose state is higher, directly shut off)

"C #" Explore Data Transfer Objects 1

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.