public
static
string ToUnicodeString(
this
string str)
{
StringBuilder strResult = new StringBuilder();
if (! string.IsNullOrEmpty(str))
{
for ( int i = 0; i < str.Length; i++)
{
strResult.Append( " \\u ");
strResult.Append((( int)str[i]).ToString( " x "));
}
}
return strResult.ToString();
}
public static string FromUnicodeString( this string str)
{
// 最直接的方法Regex.Unescape(str);
StringBuilder strResult = new StringBuilder();
if (! string.IsNullOrEmpty(str))
{
string[] strlist = str.Replace( " \\ ", "").Split( ' u ');
try
{
for ( int i = 1; i < strlist.Length; i++)
{
int charCode = Convert.ToInt32(strlist[i], 16);
strResult.Append(( char)charCode);
}
}
catch (FormatException ex)
{
return Regex.Unescape(str);
}
}
return strResult.ToString();
}
{
StringBuilder strResult = new StringBuilder();
if (! string.IsNullOrEmpty(str))
{
for ( int i = 0; i < str.Length; i++)
{
strResult.Append( " \\u ");
strResult.Append((( int)str[i]).ToString( " x "));
}
}
return strResult.ToString();
}
public static string FromUnicodeString( this string str)
{
// 最直接的方法Regex.Unescape(str);
StringBuilder strResult = new StringBuilder();
if (! string.IsNullOrEmpty(str))
{
string[] strlist = str.Replace( " \\ ", "").Split( ' u ');
try
{
for ( int i = 1; i < strlist.Length; i++)
{
int charCode = Convert.ToInt32(strlist[i], 16);
strResult.Append(( char)charCode);
}
}
catch (FormatException ex)
{
return Regex.Unescape(str);
}
}
return strResult.ToString();
}
string str =
"
我是共.产.党
";
string unicodeStr = str.ToUnicodeString();
string chStr = unicodeStr.FromUnicodeString();
string unicodeStr = str.ToUnicodeString();
string chStr = unicodeStr.FromUnicodeString();