C # wide-character encoding and decoding universal class

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.