在JS文件中直接写入一些双字节字符,有时会引发javascript编码错误,在DateChooser控件中就出现过这样的问题,现在一般的较安全的解决方法是把中文字符串转换成类似于"\u5c0f\u5bd2"这样的形式写到JS文件中,下面的代码就是实现双字节的字符和Big endian Unicode之间的相互转换。
1 /**//// <summary>
2 /// 将双字节字符转化成能更好的在网页和JS中使用的BigEndianUnicode
3 /// </summary>
4 public abstract class Class2
5 {
6
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 }
2 /// 将双字节字符转化成能更好的在网页和JS中使用的BigEndianUnicode
3 /// </summary>
4 public abstract class Class2
5 {
6
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 }