I would like to convert a string variable from UTF8 to ISO-8859-1, because for special character like ä,ö,ü, I see ?
in C#. To achieve this goal, I have found this post. But it does not work for me. I have tried to find out why....
I have observed the bytes of original and converted string in C# with this code:
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
formatter.Serialize(stream, dt2.Rows[0][0]); // I read my string from a datatable and it is utf8 encoded
byte[] bytes = stream.GetBuffer();
This line of code:
Console.WriteLine(BitConverter.ToString(bytes).Replace("-", ""));
returns:
4652495343484BEFBFBD53455A55424552454954554E47454E2020
Now, I would like to encode to ISO-8859-1. For this, I use this code:
var srcEncoding = Encoding.Default; // The original bytes are utf8 hence here "Default"
var destEncoding = Encoding.GetEncoding("ISO-8859-1");
var destBytes = Encoding.Convert(srcEncoding, destEncoding, bytes);
and then run the line of code:
Console.WriteLine(BitConverter.ToString(destBytes).Replace("-", ""));
I get the same hex code. It seems that the conversion doesn't work properly
4652495343484BEFBFBD53455A55424552454954554E47454E2020
Do you have any idea why the conversion doesn't work for me?