using System;
Using System.Text;
<summary>
Author:stone_w
date:2010.12.23
Desc: Wide character encoding and decoding
</summary>
public class Codewidthchartutility
{
Public codewidthchartutility () {}
Characters without transcoding
private static string nonencodingchats = "abcdefghijklmnopqrstuvwxyz0123456789 '!@#$%^&* () _+|-=,./; ' []{}:<>? ";
#region judge the characters that need to be converted
<summary>
To determine which characters need 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>
///wide character encoding
///1. After the code has JS control browser will automatically resolve [JS without decoding]
///2. Background control xxx.innerhtml= width Character output width string [Background control requires manual resolution]
///</SUMMARY>
///<param name= "strvalue" > the string to be encoded </PARAM>
/// <returns> the wide string </returns> after encoding;
public static string widthchartencoding (string strvalue)
{
StringBuilder sb = new StringBuilder ();
foreach (char item in strvalue)
{
if (Istowindthchart (item))//To determine the character that needs 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>
Wide character decoding
1. Manual decoding is required in the background
2.js-controlled browsers automatically decode wide characters
</summary>
<param name= "Widthstr" > Wide string </param>
<returns> characters that can be read in general </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
}
There is a problem when doing jsonp delivery, when there are special characters or Chinese will lead to data errors or garbled, just start with JS encoding and decoding and regular, are more trouble, now found a suitable solution, wide character coding, JS end will automatically parse, can handle the above problems, The following are the generic classes that you encapsulate.