帮助解决网页和JS文件中的中文编码问题的小工具

在JS文件中写入双字节字符可能引发编码错误,如DateChooser控件就出现过此类问题。较安全的解决办法是将中文字符串转换成类似“\\u5c0f\\u5bd2”的形式。文中给出了实现双字节字符和Big endian Unicode相互转换的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在JS文件中直接写入一些双字节字符,有时会引发javascript编码错误,在DateChooser控件中就出现过这样的问题,现在一般的较安全的解决方法是把中文字符串转换成类似于"\u5c0f\u5bd2"这样的形式写到JS文件中,下面的代码就是实现双字节的字符和Big endian Unicode之间的相互转换。

ExpandedBlockStart.gifContractedBlock.gif    /**//// <summary>
InBlock.gif    
/// 将双字节字符转化成能更好的在网页和JS中使用的BigEndianUnicode
ExpandedBlockEnd.gif    
/// </summary>

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    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值