Hello there, I am dealing with files encoded in UTF8 and I can't find a way to convert them into ANSI.
I've already searched in google for this since a while, and I'm not achieving the result I want to achieve if I use the code I've found on the web, which is the following:
Example of test.txt (save it as UTF8): "éàèüöä" - string filePath = "c:\\test.txt";
-
-
StreamReader fileStream = new StreamReader(filePath);
-
string fileContent = fileStream.ReadToEnd();
-
fileStream.Close();
-
-
StreamWriter ansiWriter = new StreamWriter(filePath.Replace(".txt", "-ansi.txt"), false);
-
ansiWriter.Write(fileContent, Encoding.Default);
-
ansiWriter.Close();
What I get is as result as c:\test-ansi.txt the same file (same size) and same encoding. What I can conclude is that the "Default" encoding of my machine is UTF8.
So then my question is, can you help me to correct the code snippet above so that the "ANSI" encoding will be used, instead of the "Default" one? In Encoding I can find only ASCII, UTF7, UTF16, Unicode and UTF8, I couldn't find the ANSI one...
Thanks in advance, you can't imagine how many tests I did so far...
7 92280 Plater 7,872
Recognized Expert Expert
ANSI isn't really an encoding type. I think ASCII is the type you are looking for. Although I don't see what the problem with staying in UTF8 is?
I need ANSI, because a third party application can read only ANSI encoded. I've already tried with ASCII, but it doesn't work (ascii converts accented letters in "?")...
I tried also with GetEncoding(125 0) or other code types, no way...
By the way, my previous code is wrong... It doesnt seem possible to tell Streamwriter which encoding to use... so, I tried like this, but still no success: - string filePath = "c:\\test.txt";
-
-
StreamReader fileStream = new StreamReader(filePath);
-
string fileContent = fileStream.ReadToEnd();
-
Encoding fileEncoding = fileStream.CurrentEncoding;
-
fileStream.Close();
-
-
Encoding unicode = Encoding.Unicode;
-
-
// Here I'm trying to guess which encoding to use, to save as ANSI,
-
Encoding ansi = Encoding.GetEncoding(1252);
-
-
Byte[] utf8Bytes = unicode.GetBytes(fileContent);
-
-
Byte[] ansiBytes = Encoding.Convert(unicode, ansi, utf8Bytes);
-
-
string ansiContent = ansi.GetString(ansiBytes);
-
-
// Now writes the content in ANSI
-
StreamWriter ansiWriter = new StreamWriter(filePath.Replace(".txt", "-ansi.txt"), false);
-
ansiWriter.Write(ansiContent);
-
ansiWriter.Close();
I've tried, so far, these encodings:
1250
1252
28591
28592
28593 MSDN - Encoding Class
I really don't know what to do. Notepad saves the content as ANSI, as I need. Maybe I should find an external program to do that job, and call it through c sharp, if it's not possible to achieve this task with c sharp itself... but really is it possible that it lacks this sort of conversion?
Well, now I check the other forum thread you told me... and let you know... if you have some news, do not hesitate to inform me ;) thanks again
Coool I found the solution!!! My initial code was not so wrong.
What was wrong is that I can't specify the encode in the streamwriter Write() method, but it's possible to specify it when you instantiate the StreamWriter object! So definitively the key to the problem was to write the file with the correct encoding, with streamwriter. No need to convert it before, if you do not specify the encoding, streamwriter writes it anyway in UTF8. So you MUST specify the encoding within streamwriter.
Enough talking... here the working solution: - string filePath = "c:\\test.txt";
-
-
// Reads UTF8 file
-
StreamReader fileStream = new StreamReader(filePath);
-
string fileContent = fileStream.ReadToEnd();
-
fileStream.Close();
-
-
// Now writes the content in ANSI
-
StreamWriter ansiWriter = new StreamWriter(filePath.Replace(".txt", "-ansi.txt"), false, Encoding.GetEncoding(1250));
-
ansiWriter.Write(fileContent);
-
ansiWriter.Close();
To summarize, the "magic" is in this line, when you instantiate the streamwriter object, do not forget to specify the encoding. If you wanna save as Notepad (as ANSI) use Encoding.GetEnc oding(1250). - StreamWriter ansiWriter = new StreamWriter(filePath, false, Encoding.GetEncoding(1250));
Hope this will help somebody.
Plater 7,872
Recognized Expert Expert
Very odd. Must be a regional thing. My notepad outputs to ascii(or maybe utf8?) I think
In your case, your default windows encoding is ASCII, probably because you are from the US (no accented letters). In Switzerland (where I come from) or Germany it's ANSI 1250.
Did you try to save this as ASCII? "éàèüöä" (I guess you can't, you have to save it as UTF8)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: geekboyed |
last post by:
I'm outputting a file from a mySQL server that contains 1 column that
has input it many languages. The base definition for the column is
UTF8.
I output the data into a flat file and bcp it to a sql2000 server, and
the multi-byte data does not survive the translation.
Any ideas on how to input a UTF8 flat file into the server using BCP?
...
|
by: abid |
last post by:
I need to convert DBF file to a pipe delmited text file. Can somebody
guide me with a sample C program that does this kind of conversion.
Thanks,
Abhi.
|
by: Ashutosh |
last post by:
How can i convert Word file to txt file in ASP.NET using CSharp?
|
by: Mamatha |
last post by:
Hi
I am doing small application like video capturing,i had code to create avi
file,but i want the same video file output in the form of wmv file format.
How can we convert avi file to wmv file in C#.Net?
I want the source code to convert avi file into wmv file in C#.NET.
If you know the solution please let me know.
Thanks in advance.
...
|
by: rama |
last post by:
I need to convert excel file to txt file programatically which may
contain 60000 records
i want to convert it into txt file and upload it into unix server
thanks in advance
rama
| |
by: rojavenkat81 |
last post by:
Hi All,
Please help me regarding php program for convert video file to .flv file.
I searched in many sites but i got only tool to convert video file to .flv file.But I want convert video file to .flv file by using PHP code.
Thanks
RojaVenkat
|
by: Hooyoo |
last post by:
I read the content from a file encoded with UTF8 like this:
byte data = binaryReader.ReadBytes(file.Length);
And next step, I want to transform data to ASCII format, How can I do
this?
You know, Using Encoding.UTF8.GetString(data) can get a ASCII string,
but I need a byte.
I hope you get my point. Any suggestion will be appreciated.
|
by: sharmajavaforum |
last post by:
Hai,
I have a small task.
I have an .HTML file.
I wanted to convert .HTML file into a plain .txt file.
Means after i convert the file, the .txt file should contain only the text rather
than the HTML tags information.
If you have a solution please guild me.
Bi.
|
by: poolboi |
last post by:
hi guys,
i've read a lot of thread of converting excel data into CSV files.
however, i need a perl script to convert CSV file into excel file now, if required modules is needed, it would be preferable if win32::OLE is used..
currently what i know is when i change my extension as .csv file mannually, it get changed to an excel file through...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |