帮助解决网页和JS文件中的中文编码问题的小工具
在JS文件中直接写入一些双字节字符,有时会引发javascript编码错误,在DateChooser控件中就出现过这样的问题,现在一般的较安全的解决方法是把中文字符串转换成类似于"\u5c0f\u5bd2"这样的形式写到JS文件中,下面的代码就是实现双字节的字符和Big endian Unicode之间的相互转换。
/// <summary>
/// 将双字节字符转化成能更好的在网页和JS中使用的BigEndianUnicode
/// </summary>
public abstract class Class2
{
![](/Images/OutliningIndicators/InBlock.gif)
public static void Main()
{
string test = "12,农民農民";
string entext = EncodingBigEndianUnicode(test);
WL(test);
WL(entext);
WL(DecodingBigEndianUnicode(entext));
RL();
}
public static string DecodingBigEndianUnicode(string encodedString)
{
System.Text.RegularExpressions.Regex regUnicode = new System.Text.RegularExpressions.Regex(@"\\u(?<1>[a-zA-Z0-9]{2})(?<2>[a-zA-Z0-9]{2})");
System.Text.RegularExpressions.MatchCollection mc = regUnicode.Matches(encodedString);
string s = string.Empty;
foreach(System.Text.RegularExpressions.Match m in mc)
{
byte b1 = byte.Parse(m.Groups[1].Value,System.Globalization.NumberStyles.HexNumber);
byte b2 = byte.Parse(m.Groups[2].Value,System.Globalization.NumberStyles.HexNumber);
s += System.Text.Encoding.BigEndianUnicode.GetString(new byte[]{b1,b2});
}
return s;
}
public static string EncodingBigEndianUnicode(string text)
{
string s = string.Empty;
for(int i = 0 ; i < text.Length ; i ++)
{
string s1 = text.Substring(i,1);
byte[] bs = System.Text.Encoding.BigEndianUnicode.GetBytes(s1);
s1 = @"\u";
foreach(byte b in bs)
{
string s2 = b.ToString("x");
if(s2.Length == 1)
s2 = "0" + s2;
s1 += s2;
}
s += s1;
}
return s;
}
Helper methods
}
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)