Convert string and binary arrays, display HTML files as part of the page, convert between utf8 and gb2312

Source: Internet
Author: User

1. Convert string to binary array
String content = "this is a test! ";

System. Text. unicodeencoding converter = new system. Text. unicodeencoding ();
Byte [] bytearr = converter. getbytes (content );

2. Convert the binary array into a string

System. Text. unicodeencoding converter = new system. Text. unicodeencoding ();
String spcontent = converter. getstring (bytearr );

[Files are saved to the database in binary format]

During compilation, files are saved to the database using binary data. For example, you can save the "C: \ test.html" file to the database and read it out:

1. Save the file as a stream to the database:

Int ITAG = 0;

String content = "";

Stringbuilder sb = new stringbuilder ();

String filename = @ "C: \ test.html ";
Streamreader objreader = new streamreader (filename, system. Text. encoding. getencoding ("gb2312 "));
String Sline = "";
While (Sline! = NULL)
{
Sline = objreader. Readline ();
If (Sline! = NULL)
{// You can perform corresponding processing here, such as filtering data in the file.
SB. append (Sline );

}
}

Objreader. Close ();

Content = sb. tostring (); // if you want to display the content of the entire file on the interface, you can use <% = content %> to place it in the corresponding place.

System. Text. unicodeencoding converter = new system. Text. unicodeencoding ();
Byte [] bytearr = converter. getbytes (content );

// The following code is inserted into the database,

Strinsertcmd = "insert into document (partition entid, documentcontent, addtime, moditime, status) values ('" + partition entid + "',?, '"+ Nowtime +"', '"+ nowtime +"', '00 ')";
Cmd = new oledbcommand (strinsertcm, connectionobj );
Param = new oledbparameter ("documentcontent", oledbtype. varbinary, bytearr. length, parameterdirection. Input, false, 0, 0, null, datarowversion. Current, bytearr );
Cmd. Parameters. Add (PARAM );
ITAG = cmd. executenonquery ();

If (ITAG> 0) {// success !}

2. Reading and saving as a file or string from the database is the opposite of step 1.

1. Convert gb2312 data to UTF-8 data as follows (other codes and so on ):
Public String gb2312toutf8 (string ssourse ){
String utf8_info = string. empty;
Encoding utf8 = encoding. utf8;
Encoding gb2312 = encoding. getencoding ("gb2312 ");
Byte [] unicodebytes = gb2312.getbytes (ssourse );
Byte [] asciibytes = encoding. Convert (gb2312, utf8, unicodebytes );
Char [] asciichars = new char [utf8.getcharcount (asciibytes, 0, asciibytes. Length)];
Utf8.getchars (asciibytes, 0, asciibytes. length, asciichars, 0 );
Utf8_info = new string (asciichars );
Return utf8_info;
}

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.