Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using .NET how to convert ISO 8859-1 encoded text files that contain Latin-1 accented characters to UTF-8

Tags:

I am being sent text files saved in ISO 88591-1 format that contain accented characters from the Latin-1 range (as well as normal ASCII a-z, etc.). How do I convert these files to UTF-8 using C# so that the single-byte accented characters in ISO 8859-1 become valid UTF-8 characters?

I have tried to use a StreamReader with ASCIIEncoding, and then converting the ASCII string to UTF-8 by instantiating encoding ascii and encoding utf8 and then using Encoding.Convert(ascii, utf8, ascii.GetBytes( asciiString) ) — but the accented characters are being rendered as question marks.

What step am I missing?

like image 952
Tim Avatar asked Apr 07 '10 19:04

Tim


People also ask

What is the difference between UTF-8 and ISO-8859-1?

UTF-8 is a multibyte encoding that can represent any Unicode character. ISO 8859-1 is a single-byte encoding that can represent the first 256 Unicode characters. Both encode ASCII exactly the same way.

What is encoding =' Latin-1?

ISO 8859-1 is the ISO standard Latin-1 character set and encoding format. CP1252 is what Microsoft defined as the superset of ISO 8859-1. Thus, there are approximately 27 extra characters that are not included in the standard ISO 8859-1.

How do I convert to UTF-8?

Click Tools, then select Web options. Go to the Encoding tab. In the dropdown for Save this document as: choose Unicode (UTF-8). Click Ok.


1 Answers

You need to get the proper Encoding object. ASCII is just as it's named: ASCII, meaning that it only supports 7-bit ASCII characters. If what you want to do is convert files, then this is likely easier than dealing with the byte arrays directly.

using (System.IO.StreamReader reader = new System.IO.StreamReader(fileName,                                        Encoding.GetEncoding("iso-8859-1"))) {     using (System.IO.StreamWriter writer = new System.IO.StreamWriter(                                            outFileName, Encoding.UTF8))     {         writer.Write(reader.ReadToEnd());     } } 

However, if you want to have the byte arrays yourself, it's easy enough to do with Encoding.Convert.

byte[] converted = Encoding.Convert(Encoding.GetEncoding("iso-8859-1"),      Encoding.UTF8, data); 

It's important to note here, however, that if you want to go down this road then you should not use an encoding-based string reader like StreamReader for your file IO. FileStream would be better suited, as it will read the actual bytes of the files.

In the interest of fully exploring the issue, something like this would work:

using (System.IO.FileStream input = new System.IO.FileStream(fileName,                                     System.IO.FileMode.Open,                                      System.IO.FileAccess.Read)) {     byte[] buffer = new byte[input.Length];      int readLength = 0;      while (readLength < buffer.Length)          readLength += input.Read(buffer, readLength, buffer.Length - readLength);      byte[] converted = Encoding.Convert(Encoding.GetEncoding("iso-8859-1"),                         Encoding.UTF8, buffer);      using (System.IO.FileStream output = new System.IO.FileStream(outFileName,                                          System.IO.FileMode.Create,                                           System.IO.FileAccess.Write))     {         output.Write(converted, 0, converted.Length);     } } 

In this example, the buffer variable gets filled with the actual data in the file as a byte[], so no conversion is done. Encoding.Convert specifies a source and destination encoding, then stores the converted bytes in the variable named...converted. This is then written to the output file directly.

Like I said, the first option using StreamReader and StreamWriter will be much simpler if this is all you're doing, but the latter example should give you more of a hint as to what's actually going on.

like image 80
Adam Robinson Avatar answered Sep 18 '22 04:09

Adam Robinson