Gadgets help solve the problem of Chinese web page coding and JS files of

Original link: http://www.cnblogs.com/think/archive/2005/12/27/305590.html
Some double-byte characters written directly in the JS file, sometimes erupt javascript coding errors, there have been such a problem in the DateChooser control, now generally safer solution is to convert the string to resemble a Chinese "\ u5c0f \ u5bd2 "wrote JS file such a form, the following code is to achieve mutual conversion between double-byte characters and Big endian Unicode.

ExpandedBlockStart.gif ContractedBlock.gif      / ** / /// <Summary> ///  The double-byte characters can be converted to a better use of BigEndianUnicode page and the JS /// </ Summary> 
InBlock.gif    

ExpandedBlockEnd.gif    
 

None.gif      public   abstract   class  Class2
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif
InBlock.gif        
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string test = "12,农民農民";
InBlock.gif            
string entext = EncodingBigEndianUnicode(test);
InBlock.gif            WL(test);
InBlock.gif            WL(entext);
InBlock.gif            WL(DecodingBigEndianUnicode(entext));
InBlock.gif            RL();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static string DecodingBigEndianUnicode(string encodedString)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Text.RegularExpressions.Regex regUnicode 
= new System.Text.RegularExpressions.Regex(@"\\u(?<1>[a-zA-Z0-9]{2})(?<2>[a-zA-Z0-9]{2})");
InBlock.gif            System.Text.RegularExpressions.MatchCollection mc 
= regUnicode.Matches(encodedString);
InBlock.gif            
string s = string.Empty;
InBlock.gif            
foreach(System.Text.RegularExpressions.Match m in mc)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
byte b1 = byte.Parse(m.Groups[1].Value,System.Globalization.NumberStyles.HexNumber);
InBlock.gif                
byte b2 = byte.Parse(m.Groups[2].Value,System.Globalization.NumberStyles.HexNumber);
ExpandedSubBlockStart.gifContractedSubBlock.gif                s 
+= System.Text.Encoding.BigEndianUnicode.GetString(new byte[]dot.gif{b1,b2});
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return s;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public static string EncodingBigEndianUnicode(string text)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string s = string.Empty;
InBlock.gif            
for(int i = 0 ; i < text.Length ; i ++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string s1 = text.Substring(i,1);
InBlock.gif                
byte[] bs = System.Text.Encoding.BigEndianUnicode.GetBytes(s1);
InBlock.gif                s1 
= @"\u";
InBlock.gif                
foreach(byte b in bs)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string s2 = b.ToString("x");
InBlock.gif                    
if(s2.Length == 1)
InBlock.gif                        s2 
= "0" + s2;
InBlock.gif                    s1 
+= s2;
ExpandedSubBlockEnd.gif                }

InBlock.gif                s 
+= s1;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return s;
ExpandedSubBlockEnd.gif        }

ContractedSubBlock.gifExpandedSubBlockStart.gif        
Helper methods#region Helper methods
InBlock.gif
InBlock.gif        
private static void WL(object text, params object[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(text.ToString(), args);    
ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif        
private static string RL()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Console.ReadLine();    
ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif        
private static void Break() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Diagnostics.Debugger.Break();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }

Reproduced in: https: //www.cnblogs.com/think/archive/2005/12/27/305590.html

Guess you like

Origin blog.csdn.net/weixin_30487701/article/details/94793822