天天看點

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)
            // 字元串轉換位元組數組

            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)
           

二、位元組數組轉換成字元串

// 位元組數組轉換成字元串

            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
             // 字元串轉換成流
             System.IO.MemoryStream ms1 = new System.IO.MemoryStream(strToBytes2);
             System.IO.MemoryStream ms2 = new System.IO.MemoryStream(Convert.FromBase64String(str1));
           

四、流轉換成字元串

//System.IO.MemoryStream
             // 流轉換成字元串
             var memoryToString1 = System.Text.Encoding.Default.GetString(ms2.ToArray());
             var memoryToString2 = Convert.ToBase64String(ms2.ToArray());
           

五、位元組數組轉換成流

// 位元組數組轉換成流
             System.IO.MemoryStream ms3 = new System.IO.MemoryStream(strToBytes1);
 
             System.IO.MemoryStream ms4 = new System.IO.MemoryStream(); ms4.Read(strToBytes1, 0, strToBytes1.Length);
           

六、流轉換成位元組數組

// 流轉換成位元組數組
 
 byte[] bt = ms3.ToArray();
 
 System.IO.MemoryStream ms = new System.IO.MemoryStream();
 ms.Write(bt, , (int)ms4.Length);
           
http://www.cnblogs.com/xiaoqingshe/p/5882601.html