一,测试Demo
namespace 不同编码下的中英文字符大小 { class Program { static void Main(string[] args) { ShowCode(); } private static void ShowCode() { string[] strArray = { "b","abc","乙","甲乙丙丁"}; byte[] buffer; string mode, back; foreach (string str in strArray) { for (int i=0; i<=2; i++) { if (i==0) //ASCII编码 { buffer = Encoding.ASCII.GetBytes(str); back = Encoding.ASCII.GetString(buffer, 0, buffer.Length); mode = "ASCII"; } else if(i==1) //UTF8编码 { buffer = Encoding.UTF8.GetBytes(str); back = Encoding.UTF8.GetString(buffer, 0, buffer.Length); mode = "UTF8"; } else //Unicode编码 { buffer = Encoding.Unicode.GetBytes(str); back = Encoding.Unicode.GetString(buffer, 0, buffer.Length); mode = "Unicode"; } Console.WriteLine("编码方式:{0} ;字符串:{1} ;长度:{2}", mode, str, buffer.Length); Console.WriteLine("编码后的字节数组:"); for (int j = 0; j <=buffer.Length-1; j++) { Console.WriteLine(" "+buffer[j]+" "); } Console.WriteLine("\n编码后字符为:{0} \n",back); } Console.WriteLine("----------------------------------------------------------"); } } } }
二,结果如下
b只有在Unicode下面编码为2字节,读到的byte[1]被编码为了0;ASCII码和UTF-8编码相同。
Unicode编码每个字节后面都补了个0;
对于中文:
ASCII码编码后连外星人都读不出来了;
UTF8编码出了3个字节;
Unicode出来了两个字节;