c# 编码转换
Posted zslm___
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c# 编码转换相关的知识,希望对你有一定的参考价值。
public static string GetGBKEncode(string unicodeString)
{
Encoding Gbk = Encoding.GetEncoding(936);
//创建两个不同的encoding对象
Encoding unicode = Encoding.Unicode;
//创建GBK码对象
Encoding gbk = Encoding.GetEncoding(936);
//将unicode字符串转换为字节
byte[] unicodeBytes = unicode.GetBytes(unicodeString);
//再转化为GBK码
byte[] gbkBytes = Encoding.Convert(unicode, gbk, unicodeBytes);
int i = 0;
string result = "";
while (i < gbkBytes.Length)
{
if (gbkBytes[i] <= 127)
{
result += (char)gbkBytes[i];
}
else
{
result += "%" + gbkBytes[i].ToString("X");
}
i++;
}
return result;
}
static public string uniStrToStr(string uniStr)
{
uniStr = uniStr.Replace(@"\u", @"\&H");
string[] strArray = uniStr.Split(new char[] { ‘\\‘ });
string str = "";
int num2 = strArray.Length - 1;
for (int i = 1; i <= num2; i++)
{
if (strArray[i].Length > 6)
{
string str3 = strArray[i].Substring(6);
strArray[i] = strArray[i].Substring(0, 6);
str = str + Microsoft.VisualBasic.CompilerServices.Conversions.ToString(Microsoft.VisualBasic.Strings.ChrW(Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(strArray[i]))) + str3;
}
else
{
str = str + Microsoft.VisualBasic.CompilerServices.Conversions.ToString(Microsoft.VisualBasic. Strings.ChrW(Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(strArray[i])));
}
}
return str;
}
以上是关于c# 编码转换的主要内容,如果未能解决你的问题,请参考以下文章