在JS文件中直接写入一些双字节字符,有时会引发javascript编码错误,在DateChooser控件中就出现过这样的问题,现在一般的较安全的解决方法是把中文字符串转换成类似于"\u5c0f\u5bd2"这样的形式写到JS文件中,下面的代码就是实现双字节的字符和Big endian Unicode之间的相互转换。
1
/**//// <summary>
2
/// 将双字节字符转化成能更好的在网页和JS中使用的BigEndianUnicode
3
/// </summary>
4
public abstract class Class2
5
{
6![](/Images/OutliningIndicators/InBlock.gif)
7
public static void Main()
8
{
9
string test = "12,农民農民";
10
string entext = EncodingBigEndianUnicode(test);
11
WL(test);
12
WL(entext);
13
WL(DecodingBigEndianUnicode(entext));
14
RL();
15
}
16
public static string DecodingBigEndianUnicode(string encodedString)
17
{
18
System.Text.RegularExpressions.Regex regUnicode = new System.Text.RegularExpressions.Regex(@"\\u(?<1>[a-zA-Z0-9]{2})(?<2>[a-zA-Z0-9]{2})");
19
System.Text.RegularExpressions.MatchCollection mc = regUnicode.Matches(encodedString);
20
string s = string.Empty;
21
foreach(System.Text.RegularExpressions.Match m in mc)
22
{
23
byte b1 = byte.Parse(m.Groups[1].Value,System.Globalization.NumberStyles.HexNumber);
24
byte b2 = byte.Parse(m.Groups[2].Value,System.Globalization.NumberStyles.HexNumber);
25
s += System.Text.Encoding.BigEndianUnicode.GetString(new byte[]{b1,b2});
26
}
27
return s;
28
}
29
30
public static string EncodingBigEndianUnicode(string text)
31
{
32
string s = string.Empty;
33
for(int i = 0 ; i < text.Length ; i ++)
34
{
35
string s1 = text.Substring(i,1);
36
byte[] bs = System.Text.Encoding.BigEndianUnicode.GetBytes(s1);
37
s1 = @"\u";
38
foreach(byte b in bs)
39
{
40
string s2 = b.ToString("x");
41
if(s2.Length == 1)
42
s2 = "0" + s2;
43
s1 += s2;
44
}
45
s += s1;
46
}
47
return s;
48
}
49
Helper methodsHelper methods
67
}
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](/Images/OutliningIndicators/InBlock.gif)
3
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
49
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
67
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)