C#根据汉字获取编码和根据编码获取汉字
引用命名空间:using System.Text;
代码:
string str = "中";
//根据汉字获取编码
//GB2312
byte[] GB2312Byte = Encoding.GetEncoding("GB2312").GetBytes(str);
//UTF-8
byte[] UTF8Byte = Encoding.GetEncoding("utf-8").GetBytes(str);
//UTF-7
byte[] UnicodeByte = Encoding.GetEncoding("utf-7").GetBytes(str);
//UTF-32
byte[] UTF32Byte = Encoding.GetEncoding("utf-32").GetBytes(str);
//根据对应编码获得汉字
var GB2312String = Encoding.Default.GetString(GB2312Byte);
var UTF8String = Encoding.UTF8.GetString(UTF8Byte);
var UTF7String = Encoding.UTF7.GetString(UnicodeByte);
var UTF32String = Encoding.UTF32.GetString(UTF32Byte);
//自定义编码获得汉字
byte[] bytetest = new byte[2] { 214, 208 };
//我的电脑上,默认环境为GB2312
var bytetestString = Encoding.Default.GetString(bytetest);