473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

How to change the encoding of a txt file?

Hi
I want to read a txt file and write its contents into a textbox. The problem is that the content of txt file is not in English and I have to change the encoding. So how could I change the encoding to utf-8 in my code?
Thank you.
Jan 18 '11 #1
3 6361
What is the original encoding?
Jan 19 '11 #2
Hi Samuel
The original encoding is ANSI.
By the way,I used this code, but it didn't work.

string s = File.ReadAllText("C:\\1.txt", System.Text.UTF8Encoding.UTF8);
textBox1.Text = s;
Jan 19 '11 #3
This should do the conversion fine:

Expand|Select|Wrap|Line Numbers
  1.  //str is the contents of your file.
  2. Encoding ANSI = Encoding.GetEncoding(1252);
  3.  
  4. byte[] ansiBytes = ANSI.GetBytes(str);
  5. byte[] utf8Bytes = Encoding.Convert(ANSI, Encoding.UTF8, ansiBytes);
  6.  
  7. String utf8String = Encoding.UTF8.GetString(utf8bytes);
  8.  
I presume you already know how to read a text file.

Sam.
Jan 21 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Ann | last post by:
Hi, Is there any way to Change encoding of Java Vm to ISO-8859-1? i am using Java vm along with an application called opencms. I get the following error message.. Error: the encoding of your...
1
by: pythos | last post by:
How do you change a file's last-modified date and timestamp in Python?
4
by: wil | last post by:
Dear All, In the linux platform, is there anyway I can in a C program change the file create date? Thanks, wil.
0
by: JAA | last post by:
I have made a webservice which spits out some XML data. This data gets the processing-instruction: <?xml version="1.0" encoding="uft-8"?> Is it possible to change the encoding of this...
6
by: VishalK | last post by:
Friends.. I have a HTML file. Using HTML Code, I want to change its file extension from .html to .mht. eg : TestFile.html to be converted to TestFile.mht Anybody has any idea about this ? Please...
0
by: =?Utf-8?B?TW9uaWNh?= | last post by:
Hi, How can I change default file extensions in FileUpload Control (Using ASP.NET, Web site design)? Thank you in advance. Monica.
3
by: terrychangsharp | last post by:
I think this must be easy but, alas, I can't find it! Is there any function I can use to change a file's date/time?
3
by: xke | last post by:
Other than copying the file under a different name and delete the initial file, is there any other way to change a file name in vb.net ? Thanks, xk
1
by: PSiegmann | last post by:
Hello group, if I open with streamreader a file to read in that way: StreamReader sr = new StreamReader(c:\\test.txt, System.Text.Encoding.GetEncoding(10000)); Is it possible that it can...
5
mikek12004
by: mikek12004 | last post by:
I have an xml written in a file. I want in the 1st line <code> <?xml version="1.0" encoding="iso-88-59-7"?> </code> to change the encoding to utf-8 any easy way to do that?
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.