C#将字符转换成utf8编码 GB321编码转换

public static string get_uft8(string unicodeString)
{
UTF8Encoding utf8 = new UTF8Encoding();
Byte[] encodedBytes = utf8.GetBytes(unicodeString);
String decodedString = utf8.GetString(encodedBytes);
return decodedString;
}

这边我以big5转换gb2312为例
Encoding big5 =Encoding.GetEncoding("big5");
Encoding gb2312 = Encoding.GetEncoding("gb2312");

byte[] big5b= big5.GetBytes("編程無悔!");
//关键也就是这句了
byte[] gb2312b= Encoding.Convert(big5,gb2312,big5b);

string strGb2312 = gb2312.GetString(gb2312b)

//写文件使用默认的即可,这样就能适应所有计算机了。
filePath = Path.GetDirectoryName(Application.ExecutablePath) + "\CSVFiles\" + DateTime.Now.ToString("yyyy-MM-dd_HHmmss") + ".csv";
fs = new FileStream(filePath, FileMode.Create);
sw = new StreamWriter(fs, Encoding.Default);

你可能感兴趣的:(C#将字符转换成utf8编码 GB321编码转换)