日期:2014-05-17 浏览次数:20879 次
string s = "中國";
Encoding srcEncoding = Encoding.GetEncoding("gb2312");
Encoding dstEncoding = Encoding.UTF8;
byte[] srcArr = Encoding.Convert(srcEncoding, dstEncoding, srcEncoding.GetBytes(s));
byte[] dstArr = new byte[10];
Array.Copy(srcArr, dstArr, srcArr.Length);
string s2 = dstEncoding.GetString(dstArr);
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
namespace Galsun.HH.FCX.Content
{
public class GB2312UnicodeConverter
{
/// <summary>
/// 汉字转换为Unicode编码
/// </summary>
/// <param name="str">要编码的汉字字符串</param>
/// <returns>Unicode编码的的字符串</returns>
public static string ToUnicode(string str)
{
byte[] bts = Encoding.Unicode.GetBytes(str);
string r = "";
for (int i = 0; i < bts.Length; i += 2) r += "\\u" + bts[i + 1].ToString("x").PadLeft(2, '0') + bts[i].ToString("x").PadLeft(2, '0');
return r;
}
/// <summary>
/// 将Unicode编码转换为汉字字符串
/// </summary>
/// <param name="str">Unicode编码字符串</param>
/// <returns>汉字字符串</returns>
public static string ToGB2312(string str)
{
string r = "";
MatchCollection mc = Regex.Matches(str, @"\\u([\w]{2})([\w]{2})", RegexOptions.Compiled
------解决方案--------------------
RegexOptions.IgnoreCase);
byte[] bts = new byte[2];
foreach (Match m in mc)
{
bts[0] = (byte)int.Parse(m.Groups[2].Value, NumberSt