Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of garbled codes in text by asp.net

Shulou Source: shulou.com Published: 2022-06-03 14:53:10 10月06日 Update

This article introduces the relevant knowledge of "how to solve the problem of Chinese garbled code in asp.net". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

/ solve Chinese text garbled all kinds of text coding to UTF-8 text / original path / temporary save path / new file path / new file path 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 is 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);} "how to solve the problem of garbled code in Chinese text by asp.net" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Text path garbled Chinese document problem content more knowledge practicality achievement and then dilemma reality situation article method case tape coding Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Huawei Apple Shulou Information Xiaomi