C # common usage of some string operations, C # encoding and decoding

Source: Internet
Author: User

// Obtain the location code of Chinese Characters
Byte [] array = new byte [2];
Array = system. Text. encoding. Default. getbytes ("ah ");

 

Int I1 = (short) (array [0]-''/0 '');
Int I2 = (short) (array [1]-''/0 '');

// Chinese character codes in Unicode decoding mode
Array = system. Text. encoding. Unicode. getbytes ("ah ");
I1 = (short) (array [0]-''/0 '');
I2 = (short) (array [1]-''/0 '');

// Unicode deserialization for Chinese Characters
String STR = "4a55 ";
String S1 = Str. substring (0, 2 );
String S2 = Str. substring (2, 2 );

Int T1 = convert. toint32 (S1, 16 );
Int t2 = convert. toint32 (S2, 16 );

Array [0] = (byte) T1;
Array [1] = (byte) T2;

String S = system. Text. encoding. Unicode. getstring (array );

// Decodes Chinese characters in the default mode
Array [0] = (byte) 196;
Array [1] = (byte) 207;
S = system. Text. encoding. Default. getstring (array );

// Obtain the string length
S = "Iam square gun ";
Int Len = S. length; // will output as 6
Byte [] Sarr = system. Text. encoding. Default. getbytes (s );
Len = Sarr. length; // will output as 3 + 3*2 = 9

// String Addition
System. Text. stringbuilder sb = new system. Text. stringbuilder ("");
SB. append ("I ");
SB. append ("am ");
SB. append ("square gun ");

//////////////////////////////////////// /////////////////////////////

 

String --> byte array

Byte [] DATA = syste. Text. encoding. ASCII. getbytes (string );

String --> byte

Byte data = convert. tobyte (string );

Byte [] --> string

String string = encoding. ASCII. getstring (bytes, 0, nbytessize );


C # URL encoding and decoding

--------------------------------------------------------------------------------
 
Recently, a website statistical analysis system was developed. It took a lot of time to analyze the search engine, extract search keywords from encoded URLs, and then decode them. In mainstream search engines, URLs use different character encodings. For example, the default value of Baidu is gb2312 and that of Google is UTF-8. Baidu users can specify UTF-8 encoding. Based on the two search engines, the URL may be gb2312 or UTF-8. Big Head! After several questions. A simple method that can be used for the moment.

The following is a reference clip:
// Ostr is a urlencode encoded string
Encoding gb2312 = encoding. getencoding ("gb2312 ");
Encoding utf8 = encoding. utf8;
// First, use UTF-8 for decoding.
String key = httputility. urldecode (ostr, utf8 );
// Encode the decoded characters again.
String encode = httputility. urlencode (Key, utf8). tolower ();
// Compare with the original encoding. If the encoding is inconsistent, the decoding is incorrect. Use gb2312 for decoding.
If (ostr! = Encode)
Key = httputility. urldecode (ostr, gb2312 );

This method can basically solve the URL encoding and decoding problem.

Asp.net (C # implementation) encoding and decoding (htmlencode and htmlencode) Time: Source: unknown author: Admin CLICK: 246 I want to contribute
Default. aspx:

<% @ Page validaterequest = "false" Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "test_default" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Asp.net (C #) encoding and decoding (htmlencode and htmlencode) </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: Label id = "lblshow" runat = "server" text = "label"> </ASP: Label>
<Asp: textbox id = "txtinput" runat = "server" Height = "194px" textmode = "multiline" width = "305px"> </ASP: textbox>
<Asp: button id = "btnok" runat = "server" text = "Submit" onclick = "btnok_click"/> </div>
</Form>
</Body>
</Html>

Default. aspx. CS:
 

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
************** *********
* 1. By default, users are not allowed to enter HTML tags in textbox,
* If you need to enter this parameter, set validaterequest = "false" of the page"
* 2. You can store the input HTML tags, such as <input> in the database,
* Encoding is used only during output to prevent the original output from disrupting the page layout or rendering HTML elements.
**************************************** *************/
Public partial class test_default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{

}
Protected void btnok_click (Object sender, eventargs E)
{
Lblshow. Text = htmlencode (txtinput. Text );
}
/// <Summary>
/// Encode the entered HTML and convert the carriage return and Space
/// </Summary>
/// <Param name = "str"> </param>
/// <Returns> </returns>
Public String htmlencode (string Str)
{
Return server. htmlencode (STR). Replace ("/N", "<br/>"). Replace ("","");
}

}

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.