Original link: http://hi.baidu.com/endyli/item/7bf074945de35e1f934f41fe
Source:
The interface between NDC (Networkdiskclient) and the background program communicates with the socket, sending commands.
Environment: interface: C # WinForm
Backstage: VC + +, message notification
network communication, C # is transmitted through the network byte stream, the transmission content is the protobuf of the message header. NET message. The message header is a struct structure, which is transformed into a BYTE[],PROTOBUF message before it is converted into a memory stream, then stream. Toarray[] into a byte array, which can be transmitted by the socket.
Strings string and memory stream MemoryStream and bit array byte[] cross-transfer comparison
Defines the string variable as str, the memory rheology is MS, and the bit array is BT
1. String-to-byte array
(1) byte[] Bt=system.text.encoding.default.getbytes ("string");
(2) byte[] bt=convert.frombase64string ("string");
2. String-To-stream
(1) MemoryStream ms=new MemoryStream (System.Text.Encoding.Default.GetBytes ("string"));
(2) MemoryStream ms=new MemoryStream (convert.frombase64string ("string"));
3. Stream bit Array
(1) byte[] Bt=ms. ToArray ();
(2) MemoryStream ms=new MemoryStream (); Ms. Write (Bt,0,ms. Length);
4. Flow string
(1) String str=convert.tobase64string (Ms. ToArray ());
(2) String str=system.text.encoding.default.getstring (Ms. ToArray ());
5. Bit array to string
(1) string str=system.text.encoding.default.getstring (BT);
(2) string str=convert.tobase64string (BT);
6. Bit array to stream
(1) MemoryStream ms=new MemoryStream (BT);
(2) MemoryStream ms=new MemoryStream (); Ms. Read (BT,0,BT. Lenght);
A character array in C #, a conversion between a byte array and a string (go)