ASP. Text encoding to UTF-8 text in Chinese characters

Source: Internet
Author: User

<summary>

Solve the text garbled all kinds of text encoding to UTF-8 text

</summary>

<param name= "FullPath" > Original path </param>

<param name= "Savepath" > Temporary save path </param>

<param name= "strnew" > New file path </param>

<returns> New file path </returns>

public static string GetUtf8 (String fullpath,string savepath,string strnew)

{

Encoding e =getfileencodetype (FullPath);

System.IO.File.Create (Savepath). Close ();

using (StreamReader sr = new StreamReader (FullPath, E, false))

{

String utf8info = String. Empty;


Encoding UTF8 = Encoding.UTF8;


Convert the string into a byte[].

byte[] Unicodebytes = E.getbytes (sr. ReadToEnd ());

Perform. The conversion from one encoding to the other.

byte[] Asciibytes = Encoding.convert (E, UTF8, unicodebytes);


Convert the new byte[] into a char[] and then into a string.

This was a slightly different approach to converting tillustrate

The use of Getcharcount/getchars.

char[] Asciichars = new Char[utf8. GetCharCount (asciibytes, 0, Asciibytes.length)];

UTF8. GetChars (asciibytes, 0, Asciibytes.length, asciichars, 0);

Utf8info = new String (asciichars);

using (StreamWriter SW = new StreamWriter (Savepath, False, Encoding.UTF8))

{

Sw. Write (Utf8info);

}

}

System.IO.File.Delete (FullPath);

System.IO.File.Copy (savepath,strnew,true);

System.IO.File.Delete (Savepath);

return strnew;

}







Method 2


using (FileStream fs = new FileStream (FullPath, FileMode.Open))

{

using (StreamReader sr = new StreamReader (Fs,encoding.default))

{


while (!SR. Endofstream)

{

String sline = Sr. ReadLine ();

StringBuilder B = new StringBuilder ();

B.appendline (sline);

Save to another TXT file


Using (Streamwritersw=system.io.file.appendtext (Savepath))

{

string n = b.tostring ();

Sw. WriteLine (Sline.tostring ());

}

}


}



}

System.IO.File.Delete (FullPath);

string strnewfile = FullPath;

System.IO.File.Copy (Savepath, strNewFile, true);

System.IO.File.Delete (Savepath);

using (StreamReader sr = new StreamReader (Strnewfile,encoding.default, False))

//{

using (StreamWriter SW = new StreamWriter (FullPath, Falsencoding.utf8))

//    {

Sw. Write (Server.HTMLEncode (Sr. ReadToEnd ()));

//    }

//}

System.IO.File.Delete (strNewFile);

FileInfo filenew = new FileInfo (strnewfile);

Sourcesfile.file_size = Filenew.length;

This.sourcesFileService.Save (Sourcesfile);

}



















ASP. Text encoding to UTF-8 text in Chinese characters

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.