将字符串 按照规定编码方式编码

1、ASCII 码

byte[] postBytes = Encoding.ASCII.GetBytes(param);

 ASCII及其扩展字符集 
作用:表语英语及西欧语言。 (不能编码汉字)
位数:ASCII是用7位表示的,能表示128个字符;其扩展使用8位表示,表示256个字符。 
范围:ASCII从00到7F,扩展从00到FF。

2、UTF-8 编码

 StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8); 

 byte[] buffer = Encoding.UTF8.GetBytes(str);

UTF-8:Unicode编码的一种。Unicode用一些基本的保留字符制定了三套编码方式,它们分别UTF-8,UTF-16和UTF-32。在UTF-8中,字符是以8位序列来编码的,用一个或几个字节来表示一个字符。这种方式的最大好处,是UTF-8保留了ASCII字符的编码做为它的一部分。UTF-8俗称“万国码”,可以同屏显示多语种,一个汉字占用3字节。为了做到国际化,网页应尽可能采用UTF-8编码。

当然,处理中文时http头也要改成UTF-8编码的-----加上<meta http-equiv="Content-Type" content="text/html; charset=utf-8">。 

3、GB2312编码

 Encoding myEncoding = Encoding.GetEncoding("gb2312");

 StreamReader reader = new StreamReader(receiveStream, myEncoding); 

----------------------------------------- 

byte[] buffer2 = myEncoding.GetBytes(str);

 ms.Write(buffer, 0, buffer.Length);
 ms.Flush();

------------------------------------------

 StreamReader reader = new StreamReader(receiveStream, Encoding.Default);

 StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);  

原文地址:https://www.cnblogs.com/gongyu/p/3981893.html