Code for converting strings and HTML decimal codes in Asp. Net, asp.net decimal

Source: Internet
Author: User

Code for converting strings and HTML decimal codes in Asp. Net, asp.net decimal

Asp. Net converts a string into a & # code bit encoding, or converts a & # code bit encoding string into a corresponding string content.

& # Number; this encoding is actually to convert a single character into a corresponding area code bit (number), and then add "& #" to the prefix of the area code bit, and add ";" to the suffix, the browser automatically parses the encoded string to the corresponding character.

Asp. Net string and & # encoding conversion source code and test code are as follows:

Using System; using System. text. regularExpressions; public partial class purchase_property: System. web. UI. page {// <summary> // Asp. net convert the string into a hexadecimal code bit & # encoding // </summary> // <param name = "s"> the string to be encoded with a hexadecimal code bit </param> // <returns> encoded hexadecimal code bit & # string </returns> public string StringToUnicodeCodeBit (string s) {if (string. isNullOrEmpty (s) | s. trim () = "") return ""; string r = ""; for (int I = 0; I <s. length; I ++) r ++ = "& #" + (int) s [I]). toString () + ";"; return r;} public string reMatchEvaluator (Match m) {return (char) int. parse (m. groups [1]. value )). toString () ;}/// <summary> // Asp. net convert the hexadecimal code bit & # encoding to the corresponding string // </summary> // <param name = "s"> hexadecimal code bit encoding string </param> // <returns> string corresponding to a hex encoded string </returns> public string unicodebittostring (string s) {if (string. isNullOrEmpty (s) | s. trim () = "") return ""; Regex rx = new Regex (@ "& # (\ d +);", RegexOptions. compiled); return rx. replace (s, reMatchEvaluator);} protected void Page_Load (object sender, EventArgs e) {string s = "Asp. net code bit string "; s = StringToUnicodeCodeBit (s); // convert to & # encoding Response. write (s); Response. write ("\ n"); s = UnicodeCodeBitToString (s); // & # convert encoding to string Response. write (s );}}

For the javascript version, refer to the following:

Function uncode (str) {// convert & # encoding to return str. replace (/& # (x )? ([^ &] {1, 5 });? /G, function (a, B, c) {return String. fromCharCode (parseInt (c, B? 16: 10);} function encode (str) {// convert the character to & # encode var a = [], I = 0; (; I <str. length;) a [I] = str. charCodeAt (I ++); return "& #" +. join ("; & #") + ";";}

How to remove all html code in a string from aspnet to text mode

Just clear the html tag with regular expressions. Here is a function, using System. Text. RegularExpressions;
Public static string StripHT (string strHtml)
{
Regex regex = new Regex ("<. +?> ", RegexOptions. IgnoreCase );
String strOutput = regex. Replace (strHtml ,"");
Return strOutput;
}

How does aspnet implement Chinese character encoding conversion?

For String Conversion:
I have a method here. I wrote your question!
/// <Summary>
/// Name of the preferred code page
/// </Summary>
/// <Param name = "srcName"> original encoding format </param>
/// <Param name = "convToName"> encoding format to be converted </param>
/// <Param name = "value"> string to be converted </param>
/// <Returns> returns the converted string </returns>
Public static string EnCodeCovert (string srcName, string convToName, string value)
{
System. Text. Encoding srcEncode = System. Text. Encoding. GetEncoding (srcName );
System. Text. Encoding convToEncode = System. Text. Encoding. GetEncoding (convToName );
Byte [] bytes = srcEncode. GetBytes (value );
System. Text. Encoding. Convert (srcEncode, convToEncode, bytes, 0, bytes. Length );
Return convToEncode. GetString (bytes );
}
You should have a try.

I have not tested it, but the compilation is fine. You can try it.
The reason I didn't test it is that I didn't have the environment you said !!!
Reference: www.dotnetclub.cn /? Aspdotnetdev/list-1

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.