A character array in C #, a conversion between a byte array and a string (go)

Source: Internet
Author: User
Tags array to string

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)

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.