Using system;
Using system. text;
/// <Summary>
/// Author: stone_w
/// Date: 2010.12.23
/// Desc: width character encoding and decoding
/// </Summary>
Public class codewidthchartutility
{
Public codewidthchartutility (){}
// No transcoding characters required
Private static string nonencodingchats = "abcdefghijklmnopqrstuvwxyz0123456789 '! @ # $ % ^ & * () _ + |-=,./; '[] {}:<>? ";
# Region determine the characters to be converted
/// <Summary>
/// Determine the characters to be converted
/// </Summary>
/// <Param name = "charvalue"> judgment character </param>
/// <Returns> bool </returns>
Private static bool istowindthchart (char charvalue)
{
Nonencodingchats = nonencodingchats. toupper () + nonencodingchats. tolower ();
Return nonencodingchats. indexof (charvalue) =-1;
}
# Endregion
# Region wide character encoding
/// <Summary>
/// Width character encoding
/// 1. After encoding, the browser with js control will automatically parse [js decoding is not required]
/// 2. The background control xxx. innerhtml = wide character will output the wide string as is [manual resolution is required for background control]
/// </Summary>
/// <Param name = "strvalue"> string to be encoded </param>
/// <Returns> encoded wide string </returns>
Public static string widthchartencoding (string strvalue)
{
Stringbuilder sb = new stringbuilder ();
Foreach (char item in strvalue)
{
If (istowindthchart (item) // determines the character to be converted
{
Sb. append (string. format ("u {0: x4}", (int) item ));
}
Else
{
Sb. append (item );
}
}
Return sb. tostring ();
}
# Endregion
# Region wide character decoding
/// <Summary>
/// Width character decoding
/// 1. Manual decoding is required in the background
/// The browser controlled by 2.js automatically decodes wide characters.
/// </Summary>
/// <Param name = "widthstr"> wide string </param>
/// <Returns> characters that can be understood </returns>
Public static string widthchartdecoding (string widthstr)
{
Stringbuilder sb = new stringbuilder ();
String [] _ valuelist = widthstr. split (new char [] {'', 'U'}, stringsplitoptions. removeemptyentries );
For (int I = 0; I! = _ Valuelist. length; I ++)
{
Char _ valuechar = convert. tochar (convert. touint16 (_ valuelist [I], 16 ));
Sb. append (_ valuechar. tostring ());
}
Return sb. tostring ();
}
# Endregion
}
When passing jsonp, a problem occurs. When there are special characters or Chinese characters, it may cause data errors or garbled characters. At the beginning, JavaScript code and decoding and regular expressions are available, all of them are quite troublesome. Now we have found a suitable solution. The wide character encoding and js end will automatically parse and be able to handle the above problems. Below are the general classes encapsulated by ourselves.