C#中byte数组转化为字符串

apodemakeles 2007-02-09 06:17:39
一个byte数组中存了一些从数据文件中读取的信息,里面有中文也有英文,我如何把它转化成字符串?
...全文
5995 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
tigerhai2009 2011-01-13
  • 打赏
  • 举报
回复
用七楼的办法是最好的 用国标的解码器System.Text.Encoding.GetEncoding(936).GetString(buf);
或者System.Text.Encoding.GetEncoding(“GB2312”).GetString(buf);中英文都可以解码。
withcsharp 2007-02-12
  • 打赏
  • 举报
回复
System.IO.MemoryStream ms=new System.IO.MemoryStream ();
-- DownloadFile(@"http://www.ccb.com/portal/cn/finance_quote/fundnetvalueinfo.jsp", ms);


int size = Convert.ToInt32(ms.Length);
ms.Position = 0;
Byte[] bytes = new Byte[size];
ms.Read(bytes, 0, size);
System.Text.Encoding ed= System.Text.Encoding.GetEncoding("GB2312");
string s = ed.GetString(bytes);
MessageBox.Show(s);
jxf654 2007-02-12
  • 打赏
  • 举报
回复
up
小y的CSDN博客 2007-02-12
  • 打赏
  • 举报
回复
天哪 这么复杂吗
如果byte[]={0x11,0xff,0x45};
要显示出string="11 ff 45";
用我的方法:
//显示包信息
public string dis_package(byte[] reb)
{
string temp="";
foreach(byte b in reb)
temp+=b.ToString("X2")+" ";
return temp;
}
EndPoint 2007-02-12
  • 打赏
  • 举报
回复
public static String Decode(Byte[] buf, int StartIndex, int Length, CODING Coding)
{
String str = String.Empty;
try
{
if (Coding == CODING.ASCII)
{
System.Text.ASCIIEncoding AsciiEncoder = new System.Text.ASCIIEncoding();
str = new String(AsciiEncoder.GetChars(buf, StartIndex, Length));
str = str.Split('\0')[0];
}
else if (Coding == CODING.UCS2)
{
Byte[] temp = new Byte[Length];
for (int i = 0; i < Length; )
{
// 高低位字节对调
temp[i] = buf[StartIndex + i + 1];
temp[i + 1] = buf[StartIndex + i];
i = i + 2;
}
System.Text.UnicodeEncoding UnicodeEncoder = new System.Text.UnicodeEncoding();
str = new String(UnicodeEncoder.GetChars(temp, 0, Length));
str = str.Split('\0')[0];
}
else if (Coding == CODING.GBK)
{
System.Text.UnicodeEncoding Encoder = new System.Text.UnicodeEncoding();
System.Text.Encoding en = System.Text.UnicodeEncoding.GetEncoding("gb2312");
str = new String(en.GetChars(buf, StartIndex, Length));
str = str.Split('\0')[0];
}
}
catch (Exception ex)
{
Error.Log(ex.Message);
}
return str;
}
北京的雾霾天 2007-02-12
  • 打赏
  • 举报
回复
string的构造函数就有用字节数据的重载方法啊,如
string str = new string(bytes);
zyip 2007-02-12
  • 打赏
  • 举报
回复
另外System.Convert.ToString(byteArray)也可以把byte[]装换为string
但不知道这个东东的用途,在别无选择的时候试试它吧

补充一点,如果你的数据是序列化的,以上方法都不行,需要根据你序列化的形式,反序列化
zyip 2007-02-12
  • 打赏
  • 举报
回复
二楼应该可以
如果得到的结果不正确 可能是字符集设置不正确,一般涉及到中文的字符集是utf8 unicode 和gb2312
byte[] buf=YourByteArray;
System.Text.Encoding.GetEncoding(936).GetString(buf);//gb2312
System.Text.Encoding.UTF8.GetString(buf);
System.Text.Encoding.Unicode.GetString(buf);
apodemakeles 2007-02-12
  • 打赏
  • 举报
回复
实际上我是做读取一个Dat文件的程序,里面有些中文到后来会出现相似的乱码如ASCII的 2,147类的组不成中文,可明明应该是中文的,感觉程序逻辑也没问题.我用的就是4楼的方法,仍然如此.
结帖了吧
jxf654 2007-02-10
  • 打赏
  • 举报
回复
up
jiaguoli 2007-02-09
  • 打赏
  • 举报
回复
调用ENCODING类中的GETSTGRING方法
如:
private string ConvertByteArrayToString(byte [] byteArray)
{
Encoding enc=Encoding.UTF8;
string text=enc.GetString(byteArray);
return text;
}
winxie 2007-02-09
  • 打赏
  • 举报
回复
byte数组中有英文和中文?不会吧?存的数值而已呀,不太明白LZ的意思
apodemakeles 2007-02-09
  • 打赏
  • 举报
回复
还不如以前的办法了...
jetxia 2007-02-09
  • 打赏
  • 举报
回复
System.Text.ASCIIEncoding.ASCII.GetString(bytes);
这个方法不行?

110,552

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧