转载连接:http://blog.csdn.net/c_s_d_n_2009/article/details/52583274?locationNum=9&fps=1
一、字节数组转换为字符串
- using System;
-
- namespace BasicApp
- {
- class Program
- {
- public static void Main(string[] args)
- {
- // 字节数组
- byte[] bData = {65,66,67,68};
- // 字符串
- string str = BytesToString(bData);
- // 输出
- Console.WriteLine("转换后的字符串为: {0}",str);
-
- string str2 = BytesToString(bData,"GBK");
- // 输出
- Console.WriteLine("转换后的字符串为: {0}",str2);
- Console.ReadKey(true);
- }
-
- // 字节数组转换为字符串
- public static String BytesToString(byte[] bData)
- {
- return System.Text.Encoding.GetEncoding(0).GetString(bData);
- }
-
- // 字节数组转换为字符串
- public static String BytesToString(byte[] bData,string encode)
- {
- return System.Text.Encoding.GetEncoding(encode).GetString(bData);
- }
-
- }
- }
字节数组转换为字符串的两种方法:System.Text.Encoding.GetEncoding(0).GetString(bytes)和System.Text.Encoding.GetEncoding("GBK").GetString(bytes)
转载: http://www.what21.com/sys/view/csharp-net_01_1474182326528.html
二、字节数组转换为ASCII字符串
- using System;
- using System.Text;
-
- namespace BasicApp
- {
- class Program
- {
- public static void Main(string[] args)
- {
-
- byte[] ba = new byte[] { 119,104,97,116,50,49,46,99,111,109 };
-
- string s = Encoding.ASCII.GetString(ba);
-
- Console.WriteLine(s);
-
- Console.ReadKey(true);
- }
-
- }
- }
C#字节数组转换为ASCII字符的方法:Encoding.ASCII.GetString(bytes);
转载:
http://www.what21.com/sys/view/csharp-net_01_1474184697044.html
三、字节数组与Unicode/BigEndianUnicode字符串相互转换
- using System;
- using System.Text;
-
- namespace BasicApp
- {
- class Program
- {
- public static void Main(string[] args)
- {
- string s = "IT小奋斗";
-
- byte[] bytes = Encoding.BigEndianUnicode.GetBytes(s);
- foreach (byte b in bytes){
- Console.Write(b + ",");
- }
- Console.WriteLine();
-
-
- byte[] bytes1 = new byte[] { 73,0,84,0,15,92,75,89,151,101 };
-
- string str = Encoding.Unicode.GetString(bytes1);
- Console.WriteLine(str);
-
-
- byte[] bytes2 = new byte[] { 0,73,0,84,92,15,89,75,101,151 };
-
- string str2 = Encoding.BigEndianUnicode.GetString(bytes2);
- Console.WriteLine(str2);
-
- Console.ReadKey(true);
- }
-
- }
- }
Unicode编码和BigEndianUnicode编码都使是用2个字节对一个字符进行编码,不同点就是过储存字符的顺序正好相反,即前后颠倒。
转载: http://www.what21.com/sys/view/csharp-net_01_1474185632739.html
四、按照编码(ASCII、UTF7、UTF8、Unicode、BigEndianUnicode和UTF32)将字符串转换为字节数组
- using System;
- using System.IO;
- using System.Text;
-
- namespace BasicApp
- {
- class Program
- {
- public static void Main(string[] args)
- {
- string file = "c://what21.txt";
- using (StreamWriter output = new StreamWriter(file))
- {
-
- string srcString = "IT小奋斗";
-
-
- byte[] asciiString = Encoding.ASCII.GetBytes(srcString);
- byte[] utf7String = Encoding.UTF7.GetBytes(srcString);
- byte[] utf8String = Encoding.UTF8.GetBytes(srcString);
- byte[] unicodeString = Encoding.Unicode.GetBytes(srcString);
- byte[] beUnicodeString = Encoding.BigEndianUnicode.GetBytes(srcString);
- byte[] utf32String = Encoding.UTF32.GetBytes(srcString);
-
-
- output.WriteLine("ASCII Bytes: {0}", BitConverter.ToString(asciiString));
- output.WriteLine("UTF-7 Bytes: {0}", BitConverter.ToString(utf7String));
- output.WriteLine("UTF-8 Bytes: {0}", BitConverter.ToString(utf8String));
- output.WriteLine("Unicode Bytes: {0}", BitConverter.ToString(unicodeString));
- output.WriteLine("BigEndianUnicode Bytes: {0}", BitConverter.ToString(beUnicodeString));
- output.WriteLine("UTF32 Bytes: {0}", BitConverter.ToString(utf32String));
-
-
- Console.WriteLine(BitConverter.ToString(utf8String));
- Console.WriteLine(BitConverter.ToString(utf7String));
- Console.WriteLine(BitConverter.ToString(asciiString));
- Console.WriteLine(BitConverter.ToString(unicodeString));
- Console.WriteLine(BitConverter.ToString(beUnicodeString));
- Console.WriteLine(BitConverter.ToString(utf32String));
- }
- Console.ReadKey(true);
- }
-
- }
- }
将一个字符串,按照编码ASCII、UTF7、UTF8、Unicode、BigEndianUnicode和UTF32分别转换成编码后的数组,经过BitConverter类格式化后,保存到文件并Console打印。
转载: http://www.what21.com/sys/view/csharp-net_01_1474186676521.html