Search code examples
c#stringcharacter-encodingurlencode

How to convert string from one encoding to another


I have string which is 1252 encoded,how do I convert it into UTF-8 encoding Tried Encoding.Convert but getting same 1252 encoded string when printed

var destEncoding = Encoding.UTF8; // utf-8
var srcEncoding = Encoding.GetEncoding(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

Solution

  • Code page 1252 is 8-bit. The visible escaping (%DC) looks more like it's URL encoded. See RFC3986 You can decode it like this:

        using System.Web;
    
        string inputString = "C:/Users/%DCser";     
        string decoded = HttpUtility.UrlDecode(inputString, Encoding.GetEncoding(1252));
        Console.WriteLine(decoded); 
    

    The code above should output "c:/Users/Üser" without quotes. The string in this example will be UTF16-encoded since that's .NET's default encoding. So from here you can convert it to your destination encoding.