读书人

字符串与流怎么转换

发布时间: 2012-02-14 19:19:19 作者: rapoo

字符串与流如何转换?
这个只能转英文,转汉字不行。
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);

读书人网 >C#

热点推荐