字符串与流如何转换?
这个只能转英文,转汉字不行。
string s= "abc ";
byte[] byteData = Encoding.ASCII.GetBytes(s);
Stream Astream=new MemoryStream();
Astream.Write(byteData,0,byteData.Length);
Astream.Position = 0;
byte[] bytes = new byte[1024];
int numBytesToRead = (int) Astream.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
int n = Astream.Read(bytes, numBytesRead, numBytesToRead);
if (n==0)
break;
numBytesRead += n;
numBytesToRead -= n;
}
Astream.Close();
string b=Encoding.ASCII.GetString(bytes,0,numBytesRead);
------解决方案--------------------没细看代码,不过我觉得,不应该用Encoding.ASCII吧
改Encoding.Unicode试下~
------解决方案--------------------try
byte[] byteData = Encoding.GetEncoding( "GB2312 ").GetBytes(s);
string b = Encoding.GetEncoding( "GB2312 ").GetString(bytes, 0, numBytesRead);