C# 字符串与字节数组相互转换

如果直接从System.String类中找到方法进行字符串和字节数组之间的转换,是不太可能的。为了使其之间进行转换,需要借助另外一个类型:System.Text.Encoding。这个类型提供了将C#字符串转换成字节数组的方法,也提供了将C# 字节数组转换成字符串。

System.Text.Encoding类型的默认构造函数不太可用,不过该类型提供了几种默认的静态属性。如下:

 //
        // 摘要:
        //     获取 ASCII(7 位)字符集的编码。
        //
        // 返回结果:
        //     ASCII(7 位)字符集的编码。
        public static Encoding ASCII { get; }
        //
        // 摘要:
        //     获取使用 Big Endian 字节顺序的 UTF-16 格式的编码。
        //
        // 返回结果:
        //     获取使用 Big Endian 字节顺序的 UTF-16 格式的编码对象。
        public static Encoding BigEndianUnicode { get; }
        //
        // 摘要:
        //     获取操作系统的当前 ANSI 代码页的编码。
        //
        // 返回结果:
        //     操作系统的当前 ANSI 代码页的编码。
        public static Encoding Default { get; }
        //
        // 摘要:
        //     获取使用 Little-Endian 字节顺序的 UTF-16 格式的编码。
        //
        // 返回结果:
        //     使用 Little-Endian 字节顺序的 UTF-16 格式的编码。
        public static Encoding Unicode { get; }
        //
        // 摘要:
        //     获取使用 Little-Endian 字节顺序的 UTF-32 格式的编码。
        //
        // 返回结果:
        //     使用 Little-Endian 字节顺序的 UTF-32 格式的编码对象。
        public static Encoding UTF32 { get; }
        //
        // 摘要:
        //     获取 UTF-7 格式的编码。
        //
        // 返回结果:
        //     UTF-7 格式的编码。
        public static Encoding UTF7 { get; }
        //
        // 摘要:
        //     获取 UTF-8 格式的编码。
        //
        // 返回结果:
        //     UTF-8 格式的编码。
        public static Encoding UTF8 { get; }

System.Text.Encoding   

一、字符串转换成字节数组

//System.Text.Encoding.GetBytes(String str)
            //1. 字符串转换字节数组

            string str1 = "character string1";
            var strToBytes1 = System.Text.Encoding.UTF8.GetBytes(str1);

            string str2 = "character string2";
            var strToBytes2 = System.Text.Encoding.Default.GetBytes(str2);

            string str3 = "character string3";
            var strToBytes3 = System.Text.Encoding.Unicode.GetBytes(str3);

            string str4 = "character string4";
            var strToBytes4 = System.Text.Encoding.ASCII.GetBytes(str4);

            string str5 = "character string5";
            var strToBytes5 = System.Text.Encoding.UTF32.GetBytes(str5);

            string str6 = "character string6";
            var strToBytes6 = System.Text.Encoding.UTF7.GetBytes(str6);

System.Text.Encoding.GetBytes(String str)

二、字节数组转换成字符串

//2. 字节数组转换成字符串

            Console.Write("strToBytes1 使用UTF8编码转换成字符串:");
            var byteToString1 = System.Text.Encoding.UTF8.GetString(strToBytes1);
            Console.WriteLine(byteToString1);
            Console.Write("strToBytes1 使用Default编码转换成字符串:");
            var byteToString2 = System.Text.Encoding.Default.GetString(strToBytes2);
            Console.WriteLine(byteToString2);
            Console.Write("strToBytes1 使用Unicode编码转换成字符串:");
            var byteToString3 = System.Text.Encoding.Unicode.GetString(strToBytes3);
            Console.WriteLine(byteToString3);
            Console.Write("strToBytes1 使用ASCII编码转换成字符串:");
            var byteToString4 = System.Text.Encoding.ASCII.GetString(strToBytes4);
            Console.WriteLine(byteToString4);
            Console.Write("strToBytes1 使用UTF32编码转换成字符串:");
            var byteToString5 = System.Text.Encoding.UTF32.GetString(strToBytes5);
            Console.WriteLine(byteToString5);
            Console.Write("strToBytes1 使用UTF7编码转换成字符串:");
            var byteToString6 = System.Text.Encoding.UTF7.GetString(strToBytes6);
            Console.WriteLine(byteToString6);

System.Text.Encoding.GetString(byte[] byte)

三、 字符串转换成流

//System.IO.MemoryStream
2             //3. 字符串转换成流
3             System.IO.MemoryStream ms1 = new System.IO.MemoryStream(strToBytes2);
4             System.IO.MemoryStream ms2 = new System.IO.MemoryStream(Convert.FromBase64String(str1));

四、流转换成字符串

 //System.IO.MemoryStream
2             //4. 流转换成字符串
3             var memoryToString1 = System.Text.Encoding.Default.GetString(ms2.ToArray());
4             var memoryToString2 = Convert.ToBase64String(ms2.ToArray());

五、字节数组转换成流

 //5. 字节数组转换成流
2             System.IO.MemoryStream ms3 = new System.IO.MemoryStream(strToBytes1);
3 
4             System.IO.MemoryStream ms4 = new System.IO.MemoryStream(); ms4.Read(strToBytes1, 0, strToBytes1.Length);

六、流转换成字节数组

//6. 流转换成字节数组
2 
3 byte[] bt = ms3.ToArray();
4 
5 System.IO.MemoryStream ms = new System.IO.MemoryStream();
6 ms.Write(bt, 0, (int)ms4.Length);

http://www.cnblogs.com/xiaoqingshe/p/5882601.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值