Encoding in ASP. NET

Source: Internet
Author: User
Tags response code

Using system;
Using system. text;
Using system. IO;
Namespace charandstring
{
Class class1
{
Static void main (string [] ARGs)
{
// The content of string is a unicode string:
String STR = "Chinese 123 ";
// The length is 5 because it is 5 characters.
Console. writeline (Str. Length );
// Convert strings and byte strings to each other
// Obtain the byte according to gb2312 (obtain the multi-byte string)
Encoding GB = encoding. getencoding ("gb2312 ");
Byte [] bytes = GB. getbytes (STR );
Char [] chars = GB. getchars (bytes); // obtain the character array from byte according to gb2312
STR = GB. getstring (bytes); // obtain the Unicode string from the byte according to gb2312
// There are two methods to write a string into a text file according to certain encoding:
// Method 1: Use the stream class to write a byte string that has been converted according to the specified Encoding
Streamwriter Sw = new streamwriter ("1.txt ");
Sw. Write (chars );
Sw. Close ();
// Method 2: Construct the specified encoded writer to write the string.
Streamwriter sw2 = new streamwriter ("2.txt", false, GB );
Sw2.write (STR );
Sw2.close ();
/* The last obtained 1.txt and 2.txt are both 7 bytes */}
}
}

Using system;
Using system. text;
Namespace wrongcode
{
Class class1
{
Static void main (string [] ARGs)
{
// The content of string is a unicode string:
String STR = "Chinese 123 ";
// Obtain the byte according to gb2312 (obtain the multi-byte string)
Encoding client = encoding. getencoding ("gb2312 ");
Byte [] bytes = client. getbytes (STR );
// The server correctly decodes data according to gb2312
Encoding serverok = encoding. getencoding ("gb2312 ");
String strserver = serverok. getstring (bytes );
Console. writeline ("the correctly converted string is:" + strserver );
// The server decodes Unicode errors.
Strserver = encoding. Unicode. getstring (bytes );
Console. writeline ("the character string after error conversion is:" + strserver );
}
}
}

Using system;
Using system. text;
Namespace wrongcode
{
Class class1
{
Static void main (string [] ARGs)
{
// The content of string is a unicode string:
String STR = "Chinese 123 ";
// Obtain the byte according to gb2312 (obtain the multi-byte string)
Encoding client = encoding. getencoding ("gb2312 ");
Byte [] bytes = client. getbytes (STR );
// The server correctly decodes data according to gb2312
Encoding serverok = encoding. getencoding ("gb2312 ");
String strserver = serverok. getstring (bytes );
Console. writeline ("the correctly converted string is:" + strserver );
// The server decodes Unicode errors.
Strserver = encoding. Unicode. getstring (bytes );
Console. writeline ("the character string after error conversion is:" + strserver );
}
}
}


Private void page_load (Object sender, system. eventargs E)
{
Response. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
If (! Page. ispostback)
{
Response. Write ("no characters have been entered! <Br> ");
}
Else
{
Label_1.text = "";
Label_2.text = "";
Label_0.text = convert. tostring (textbox_1.text). Length );
// ASCII code output
Bytefunction (textbox_1.text );
// Response code output
Charfunction (textbox_1.text );
// Html encoding output
Htmlfunction (textbox_1.text );
Gb2312function ("");
}
}
// ASCII output function
Void bytefunction (string Str)
{
Byte [] mybytes = system. Text. encoding. ASCII. getbytes (STR );
For (INT I = 0; I <Str. length; I ++)
{
Label_2.text + = convert. tostring (mybytes [I]) + "";
}
}
// Response code output function
Void charfunction (string Str)
{
Byte [] mybytes = system. Text. encoding. ASCII. getbytes (STR );
Char [] mychars = system. Text. encoding. ASCII. getchars (mybytes );
// Label_2.text = new string (mychars );
For (INT I = 0; I <Str. length; I ++)
{
Label_1.text + = convert. tostring (mychars [I]) + "";
}
}
// Html encoding output function
Void htmlfunction (string Str)
{
String str_html = (server. htmlencode (STR ));
Label_3.text = str_html;
Label_4.text = server. htmlencode (str_html); // encode the string to be displayed in the browser.
}
Void gb2312function (string Str)
{
String utfinfo = "document. Write (/" alert ('aa, are you okay ?? ');/");";
String gb2312info = "";
Encoding utf8 = encoding. utf8;
Encoding gb2312 = encoding. getencoding ("gb2312 ");
Byte [] unicodebytes = utf8.getbytes (utfinfo );
Byte [] asciibytes = encoding. Convert (utf8, gb2312, unicodebytes); // convert the new byte [] into a char [] and then into a string.
Char [] asciichars = new char [gb2312.getcharcount (asciibytes, 0, asciibytes. Length)];
Gb2312.getchars (asciibytes, 0, asciibytes. length, asciichars, 0 );
Gb2312info = new string (asciichars );
Label_5.text = gb2312info;
}

Production text Verification Code
Private void page_load (Object sender, system. eventargs E)
{
// Obtain the gb2312 encoding page (table)
Encoding GB = encoding. getencoding ("gb2312 ");
// Call the function to generate four random Chinese character codes
Object [] bytes = createregioncode (4 );
// Decodes Chinese Characters Based on the byte array encoded by Chinese Characters
String str1 = GB. getstring (byte []) convert. changetype (Bytes [0], typeof (byte []);
String str2 = GB. getstring (byte []) convert. changetype (Bytes [1], typeof (byte []);
String str3 = GB. getstring (byte []) convert. changetype (Bytes [2], typeof (byte []);
String str4 = GB. getstring (byte []) convert. changetype (Bytes [3], typeof (byte []);
String strkey = str1 + str2 + str3 + str4;
Session ["regcode"] = strkey;
Byte [] DATA = GB. getbytes (strkey );
Response. contentencoding = GB;
Response. outputstream. Write (data, 0, Data. Length );
}
/*
This function randomly creates a hexadecimal byte array containing two elements within the Chinese character encoding range. Each byte array represents a Chinese character and
The four byte arrays are stored in the object array.
Parameter: strlength, indicating the number of Chinese characters to be generated
*/
Public static object [] createregioncode (INT strlength)
{
// Define a string array to store the components of Chinese character encoding
String [] rbase = new string [16] {"0", "1", "2", "3", "4", "5", "6 ", "7", "8", "9", "A", "B", "C", "D", "E", "F "};
Random RND = new random ();
// Define an object array
Object [] bytes = new object [strlength];
/** // * Generates a hexadecimal byte array containing two elements at a time in each loop, and puts it into the bject array.
Each Chinese Character consists of four location codes.
The first element of the byte array is the 1st bits and 2nd bits.
The second element of the byte array is the 3rd bits and 4th bits.
*/
For (INT I = 0; I <strlength; I ++)
{
// Code 1st bits
Int R1 = RND. Next (11,14 );
String str_r1 = rbase [R1]. Trim ();
// Code 2nd bits
RND = new random (R1 * unchecked (INT) datetime. Now. ticks) + I); // Replace the seed of the random number generator to avoid repeated values.
Int R2;
If (r1 = 13)
{
R2 = RND. Next (0, 7 );
}
Else
{
R2 = RND. Next (0, 16 );
}
String str_r2 = rbase [R2]. Trim ();
// Code 3rd bits
RND = new random (R2 * unchecked (INT) datetime. Now. ticks) + I );
Int R3 = RND. Next (10, 16 );
String str_r3 = rbase [R3]. Trim ();
// Code 4th bits
RND = new random (R3 * unchecked (INT) datetime. Now. ticks) + I );
Int R4;
If (R3 = 10)
{
R4 = RND. Next (1, 16 );
}
Else if (R3 = 15)
{
R4 = RND. Next (0, 15 );
}
Else
{
R4 = RND. Next (0, 16 );
}
String str_r4 = rbase [R4]. Trim ();
// Define the random Chinese character location code generated by storing two byte Variables
Byte byte1 = convert. tobyte (str_r1 + str_r2, 16 );
Byte byte2 = convert. tobyte (str_r3 + str_r4, 16 );
// Store two byte variables in the byte array
Byte [] str_r = new byte [] {byte1, byte2 };
// Put the byte array of the generated Chinese character into the object Array
Bytes. setvalue (str_r, I );
}
Return bytes;
}
}

Save the image to XML
Private void btnupload_click (Object sender, system. eventargs E)
{
// Get the file name to be uploaded
String strfilepathname = lofile. postedfile. filename;
String strfilename = path. getfilename (strfilepathname );
Int filelength = lofile. postedfile. contentlength;
If (filelength <= 0)
Return;
Try
{
Byte [] filebytearray = new byte [filelength]; // temporary storage of byte Arrays for image files
Stream streamobject = lofile. postedfile. inputstream; // create a data stream object
// Read image file data. filebytearray is the data storage body, 0 is the Data Pointer position, and filelne is the data length.
Streamobject. Read (filebytearray, 0, filelength );
String filename = server. mappath (". // writexml. xml"); // the file to be opened
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (filename );
Xmlnode root = xmldoc. selectsinglenode ("dbimage"); // search for <dbguest>
Xmlnodelist xnl = xmldoc. selectsinglenode ("dbimage"). childnodes;
Int nindex = xnl. count;
// Add the following Node
Xmlelement xe1 = xmldoc. createelement ("image"); // create a <user> node
Xmlelement xesub1 = xmldoc. createelement ("imageid ");
Xesub1.innertext = nindex. tostring (); // set the text node
Xe1.appendchild (xesub1); // Add it to the <user> node
Xmlelement xesub2 = xmldoc. createelement ("imagecontenttype ");
Xesub2.innertext = lofile. postedfile. contenttype;
Xe1.appendchild (xesub2 );
Xmlelement xesub3 = xmldoc. createelement ("imagesize ");
Xesub3.innertext = filelength. tostring ();
Xe1.appendchild (xesub3 );
Xmlelement xesub4 = xmldoc. createelement ("imagedescription ");
Xesub4.innertext = tbdescription. text;
Xe1.appendchild (xesub4 );
Xmlelement xesub5 = xmldoc. createelement ("imagedata ");
Xesub5.innertext = convert. tobase64string (filebytearray );
Xe1.appendchild (xesub5 );
Root. appendchild (xe1); // Add it to the <dbguest> node.
Xmldoc. Save (filename );
Response. Redirect ("showallimg. aspx ");
}
Catch
{
}
}
Private void page_load (Object sender, system. eventargs E)
{
String filename = server. mappath ("./writexml. xml"); // the file to be opened
Dataset DS = new dataset ();
DS. readxml (filename );
Dgshow. datasource = Ds. Tables ["image"]. defaultview;
Dgshow. databind ();
}
Private void page_load (Object sender, system. eventargs E)
{
Int imgid = convert. toint32 (request. querystring ["ID"]); // ID is the image ID
// Create a database link
String filename = server. mappath (". // writexml. xml"); // the file to be opened
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (filename );
Xmlnodelist node = xmldoc. selectsinglenode ("// image [imageid = '" + imgid. tostring () + "']"). childnodes;
If (node! = NULL)
{
String strtype = node. Item (1). innertext;
String strdata = node. Item (4). innertext;
Int nsize = int. parse (node. Item (2). innertext );
Response. contenttype = strtype; // set the output file type
// Binary number of output image files
Response. outputstream. Write (convert. frombase64string (strdata), 0, nsize );
Response. End ();
// It can also be saved as an image
// Filestream FS = new filestream (@ "C:/AA. BMP", filemode. openorcreate, fileaccess. Write );
// Fs. Write (convert. frombase64string (strdata), 0, nsize );
// Fs. Close ();
}

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.