473,418 Members | 2,171 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,418 software developers and data experts.

how to convert UTF8 file into ANSI?

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): "éàèüöä"

Expand|Select|Wrap|Line Numbers
  1. string filePath = "c:\\test.txt";
  2.  
  3. StreamReader fileStream = new StreamReader(filePath);
  4. string fileContent = fileStream.ReadToEnd();
  5. fileStream.Close();
  6.  
  7. StreamWriter ansiWriter = new StreamWriter(filePath.Replace(".txt", "-ansi.txt"), false);
  8. ansiWriter.Write(fileContent, Encoding.Default);
  9. 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...
Mar 2 '09 #1
7 92114
Plater
7,872 Expert 4TB
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?
Mar 2 '09 #2
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(1250) or other code types, no way...
Mar 2 '09 #3
Plater
7,872 Expert 4TB
(oops, there is an ANSI encoding, just not listed in .NET)
1252 didn't work?
It sounds like you just need an 8bit encoding (ascii is 7bit)

You could try someone of the codepages mentioned here:
http://bytes.com/groups/net-c/436214...-ansi-encoding
Mar 2 '09 #4
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:

Expand|Select|Wrap|Line Numbers
  1. string filePath = "c:\\test.txt";
  2.  
  3. StreamReader fileStream = new StreamReader(filePath);
  4. string fileContent = fileStream.ReadToEnd();
  5. Encoding fileEncoding = fileStream.CurrentEncoding;
  6. fileStream.Close();
  7.  
  8. Encoding unicode = Encoding.Unicode;
  9.  
  10. // Here I'm trying to guess which encoding to use, to save as ANSI,
  11. Encoding ansi = Encoding.GetEncoding(1252);
  12.  
  13. Byte[] utf8Bytes = unicode.GetBytes(fileContent);
  14.  
  15. Byte[] ansiBytes = Encoding.Convert(unicode, ansi, utf8Bytes);
  16.  
  17. string ansiContent = ansi.GetString(ansiBytes);
  18.  
  19. // Now writes the content in ANSI
  20. StreamWriter ansiWriter = new StreamWriter(filePath.Replace(".txt", "-ansi.txt"), false);
  21. ansiWriter.Write(ansiContent);
  22. 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
Mar 2 '09 #5
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:

Expand|Select|Wrap|Line Numbers
  1. string filePath = "c:\\test.txt";
  2.  
  3. // Reads UTF8 file
  4. StreamReader fileStream = new StreamReader(filePath);
  5. string fileContent = fileStream.ReadToEnd();
  6. fileStream.Close();
  7.  
  8. // Now writes the content in ANSI
  9. StreamWriter ansiWriter = new StreamWriter(filePath.Replace(".txt", "-ansi.txt"), false, Encoding.GetEncoding(1250));
  10. ansiWriter.Write(fileContent);
  11. 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.GetEncoding(1250).

Expand|Select|Wrap|Line Numbers
  1. StreamWriter ansiWriter = new StreamWriter(filePath, false, Encoding.GetEncoding(1250));
Hope this will help somebody.
Mar 2 '09 #6
Plater
7,872 Expert 4TB
Very odd. Must be a regional thing. My notepad outputs to ascii(or maybe utf8?) I think
Mar 2 '09 #7
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)
Mar 2 '09 #8

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

Similar topics

2
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...
4
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.
1
by: Ashutosh | last post by:
How can i convert Word file to txt file in ASP.NET using CSharp?
1
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...
1
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
3
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...
1
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...
1
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 ...
32
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.