Dennis von Bargen
Dennis von Bargen

Reputation: 11

Unable to force ANSI encoding (windows-1252)

I'm trying to save two text files in ANSI encoding for later processing by a legacy system. However when I save it in the correct encoding, it still saves as a UTF-8 file.

I've tried the following:

File.WriteAllLines(filePath, lines, Encoding.GetEncoding(1252));
File.WriteAllLines(filePath, lines, Encoding.GetEncoding("windows-1252"));
using (StreamWriter writer = new StreamWriter(fileName, false, Encoding.GetEncoding(1252)))
{
    foreach (string line in lines)
    {
        writer.WriteLine(line);
    }
}

I've also tried converting an existing utf-8 file to ansi

File.WriteAllBytes(fileName, Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding(1252), File.ReadAllBytes(fileName)));

None of the above solutions have worked; they're still UTF-8. The only way I managed to make it save as ANSI was by inserting swedish characters like åäö, which is a hack I cannot use for one of the files.

I'm at a loss. Has anyone got a solution to this issue?

We're on .NET Framework 4.5, C# 7.3

Upvotes: 0

Views: 3786

Answers (2)

Dennis von Bargen
Dennis von Bargen

Reputation: 11

I did a thorough investigation and found that it works, just not in the way I expected. As @jdweng said: Nothing in the data contains the encoding, you're just saving bytes. For the most part you're saving regular ASCII characters, so when you for instance open Notepad++ to read it, it will default to whatever encoding it prefers, unless you have a special character that hints to the program which encoding to use.

I encoded a file in four encodings (default (UTF-8), ANSI, ASCII and UTF-8-BOM) and opened up all files in a hex editor and found that in most cases the ä in these files determined which decoder to use in Notepad++.

So if the legacy system uses an ANSI decoder, it should be able to open an "ANSI" encoded file without special characters. Despite it showing up as UTF-8 in Notepad++.

Upvotes: 1

Matthew Watson
Matthew Watson

Reputation: 109567

It definitely works. Try the following program:

using System.IO;
using System.Text;

namespace Demo
{
    static class Program
    {
        static void Main()
        {
            string filePath = @"E:\tmp\test"; // Put your path here.

            string[] lines = { "ÿ" };

            File.WriteAllLines(filePath + ".1.bin", lines, Encoding.GetEncoding(1252));
            File.WriteAllLines(filePath + ".2.bin", lines);
        }
    }
}

Run the program and then inspect the contents of the files in a binary editor.

You will see the following:

test.1.bin contains: FF 0D 0A
test.2.bin contains: 0C BF 0D 0A

(Note: If you drag and drop a ".bin" file into Visual Studio, it will open it in binary mode.)

Upvotes: 0

Related Questions