sk7730
sk7730

Reputation: 736

Convert text file from known (1252) encoding to UTF8 File

I have a file which has text with windows-1252 encoding. How to convert it into UTF8 file format?

Upvotes: 1

Views: 235

Answers (1)

Matt Coubrough
Matt Coubrough

Reputation: 3829

The Encoding class supports conversions.

byte[] asciiBytes = File.ReadAllBytes("C:\\ascii.txt");

Encoding ASCII_1252 = Encoding.GetEncoding("windows-1252");

byte[] utf8Bytes = Encoding.Convert(ASCII_1252, Encoding.UTF8, asciiBytes);

File.WriteAllBytes("C:\\utf8.txt", utf8Bytes);

Note that GetEncoding() relies on the underlying platform to support most code pages as explained here: http://msdn.microsoft.com/en-us/library/t9a3kf7c(v=vs.100).aspx

Upvotes: 2

Related Questions