As Jon Skeet already pointed out, the string itself has no encoding, it's the byte[]
that has an encoding. So you need to know which encoding has been applied to your string, based on this you can retrieve the byte[]
for the string and convert it to the desired encoding. The resulting byte[]
can then be further processed (e.g. written to a file, returned in a HttpRequest, ...).
// get the correct encodings
var srcEncoding = Encoding.UTF8; // utf-8
var destEncoding = Encoding.GetEncoding(1252); // windows-1252
// convert the source bytes to the destination bytes
var destBytes = Encoding.Convert(srcEncoding, destEncoding, srcEncoding.GetBytes(srcString));
// process the byte[]
File.WriteAllBytes("myFile", destBytes); // write it to a file OR ...
var destString = destEncoding.GetString(destBytes); // ... get the string