user1861857
user1861857

Reputation: 318

How to convert file to unix or windows in c# when I don't know the original encoding

In c#, I have a file which has Unix line endings(\r) I need to replace those to Windows (\r\n). But,

1 - I don't know the original file encoding (utf-8, unicode, iso8852-1, etc) and

2 - I don't know how big the original file may be.

The first point is important - I cannot simply read and write each line using a StreamWriter because I don't know the original encoding.

How can I achieve this?

Upvotes: 0

Views: 891

Answers (1)

Vivek Nuna
Vivek Nuna

Reputation: 1

private void Unix2Dos(string fileName)
{
    const byte CR = 0x0D;
    const byte LF = 0x0A;
    byte[] DOS_LINE_ENDING = new byte[] { CR, LF };
    byte[] data = File.ReadAllBytes(fileName);
    using (FileStream fileStream = File.OpenWrite(fileName))
    {
        BinaryWriter bw = new BinaryWriter(fileStream);
        int position = 0;
        int index = 0;
        do
        {
            index = Array.IndexOf<byte>(data, LF, position);
            if (index >= 0)
            {
                if ( ( index > 0 ) && (data[index - 1] == CR ))
                {
                    // already dos ending
                    bw.Write(data, position, index - position + 1);
                }
                else
                {
                    bw.Write(data, position, index - position);
                    bw.Write(DOS_LINE_ENDING);
                }
                position = index + 1;
            }
        }
        while (index > 0);
        bw.Write(data, position, data.Length - position);
       fileStream.SetLength(fileStream.Position);
    }
}

Reference: http://csharp-goodies.blogspot.com/2011/02/convert-files-from-dos-to-unix-and-back.html

Upvotes: 2

Related Questions