Conversion between C string string and memory stream MemoryStream and bit array byte []

  • 2021-10-11 19:22:21
  • OfStack

The string variable is defined as str, the memory stream variable as ms, and the bit array as bt

1. String-to-bit arrays

(1)byte[] bt=System.Text.Encoding.Default.GetBytes(" String ");
(2)byte[] bt=Convert.FromBase64String(" String ");

2. String bypass

(1)MemoryStream ms=new MemoryStream(System.Text.Encoding.Default.GetBytes(" String "));
(2)MemoryStream ms=new MemoryStream(Convert.FromBase64String(" String "));

3. Stream bit arrays

(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 Converting

(1)MemoryStream ms=new MemoryStream(bt);
(2)MemoryStream ms=new MemoryStream();ms.Read(bt,0,bt.Length);

Summary: It can be seen that byte [] acts as a transition between string string and stream MemoryStream, and both string and MemoryStream conversions must be converted to byte [] first.

Related articles: