+ -
当前位置:首页 → 问答吧 → byte[] 数组 如何转换成字符串string

byte[] 数组 如何转换成字符串string

时间:2011-12-18

来源:互联网

byte[] b=new byte[2];
b[0]=00;
b[1]=10;
string s="";
如何让让byte数组转换成string 类型的字符串? 求解?

作者: chengliwangtao   发布时间: 2011-12-18

C# code

        public static string ByteToString(byte[] bytes)
        {
            StringBuilder strBuilder = new StringBuilder();
            foreach (byte byt in bytes)
            {
                strBuilder.AppendFormat("{0:X2}", byt);
            }
            return strBuilder.ToString();
        }

作者: wnyxy   发布时间: 2011-12-18

同一个字节序列(byte数组),需要专门的转换规则,才能转成string,这个转换规则就是编码方式:
比如utf-8编码时:
string s=Encoding.UTF8.GetString(bytes);

作者: dalmeeme   发布时间: 2011-12-18