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; }
|
请发表评论