Conversion) Conversion between escape and C # In Javascript

Source: Internet
Author: User
Modify:

Public static string escape (string S)
{
Stringbuilder sb = new stringbuilder ();
Byte [] BA = system. Text. encoding. Unicode. getbytes (s );
For (INT I = 0; I <ba. length; I + = 2)
{
If (BA [I + 1] = 0)
{
// Numbers, uppercase and lowercase letters, and "+-*/. _" remain unchanged
If (
(BA [I]> = 48 & Ba [I] <= 57)
| (BA [I]> = 64 & Ba [I] <= 90)
| (BA [I]> = 97 & Ba [I] <= 122)
| (BA [I] = 42 | Ba [I] = 43 | Ba [I] = 45 | Ba [I] = 46 | Ba [I] = 47 | Ba [I] = 95)
) // Remain unchanged
{
SB. append (encoding. Unicode. getstring (BA, I, 2 ));

}
Else // % XX format
{
SB. append ("% ");
SB. append (BA [I]. tostring ("X2 "));
}
}
Else
{
SB. append ("% u ");
SB. append (BA [I + 1]. tostring ("X2 "));
SB. append (BA [I]. tostring ("X2 "));
}
}
Return sb. tostring ();

}

Source URL javascript: Escape encoding and C # usage problems. Onsubmit = "form1.text1. value = escape (form1.text1. Value );"

 

 

. CS Processing

 

  String A = Httputility. urldecode (text1.value );
Response. Write ();

 

 

// C # implement escape encoding: (used in Ajax) and decode it with Unescape in Aspx.
  Private   String Escape ( String S)
{
Stringbuilder sb =   New Stringbuilder ();
Byte [] Ba = System. Text. encoding. Unicode. getbytes (s );
For ( Int I =   0 ; I < Ba. length; I + =   2 )
{
SB. append ( " % U " );
SB. append (BA [I +   1 ]. Tostring ( " X2 " ));

SB. append (BA [I]. tostring ("X2"));
}
ReturnSB. tostring ();

}

Other methods ---------------------------- encodeuricomponent ()/decodeuricomponent () encodeuri ()/decodeuri () note that the above two JavaScript Functions are mostly encoded as UTF-8. If the page is not encoded as UTF-8, additional processing is required. Asp.net sends data to Javascript when gb2312 is used on the page. encodestring = httputility. urlencode ("The question is in the http://www.gyzs.net/", system. text. encoding. utf8) receives <SCRIPT> document. write (decodeuricomponent ('<% = encodestring %>'); </SCRIPT> The following JS script is available on the javascrui send data to Asp.net page <SCRIPT> Document. Write ("<a href = '? T = "+ encodeuri ('in question is in question http://www.gyzs.net') +" '> click me </a> "); </SCRIPT> If the web. if gb2312 is configured in config, the following processing is required: response. write (httputility. urldecode (server. urlencode (request ["T"]), system. text. encoding. utf8); If the web. if UTF-8 is used in config, you can directly use request ["T"]. In addition, note that when the form is submitted using the POST method, URL encoding is performed on the data in the form. Be sure not to repeat it.

 

 

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.