SoFunction
Updated on 2025-03-06

Convert C# string string, memory stream MemoryStream and bit array byte[]

Define string variable as str, memory stream variable as ms, and bit array as bt

1. To string to bit array

Copy the codeThe code is as follows:
(1)byte[] bt=("String");
(2) byte[] bt=Convert.FromBase64String("String");

2. String transfer

Copy the codeThe code is as follows:
(1)MemoryStream ms=new MemoryStream(("String"));
(2)MemoryStream ms=new MemoryStream(Convert.FromBase64String("String"));

3. Flow bit array

Copy the codeThe code is as follows:
(1)byte[] bt=();
(2)MemoryStream ms=new MemoryStream();(bt,0,);

4. Stream strings

Copy the codeThe code is as follows:
(1)string str=Convert.ToBase64String(());
(2)string str=(());

5. Turn bit array into string

Copy the codeThe code is as follows:
(1)string str=(bt);
(2)string str=Convert.ToBase64String(bt);

6. Bit array transfer

Copy the codeThe code is as follows:
(1)MemoryStream ms=new MemoryStream(bt);
(2)MemoryStream ms=new MemoryStream();(bt,0,);

Summarize:It can be seen that byte[] plays a transition role between string string and stream MemoryStream. Both string and MemoryStream conversions must be converted to byte[] first.