C#转换字符串从UTF-8到ISO-8859-1(Latin1的)H [英] C# Convert string from UTF-8 to ISO-8859-1 (Latin1) H
问题描述
我知道这已经问过!
我用Google搜索这个话题,我已经看过所有的答案,但我还是不明白这一点。
I have googled on this topic and I have looked at every answer, but I still don't get it.
基本上我需要为UTF-8字符串转换为ISO-8859-1和我用下面的code的:
Basically I need to convert UTF-8 string to ISO-8859-1 and I do it using following code:
Encoding iso = Encoding.GetEncoding("ISO-8859-1");
Encoding utf8 = Encoding.UTF8;
string msg = iso.GetString(utf8.GetBytes(Message));
我的源字符串是
My source string is
Message = "ÄäÖöÕõÜü"
但不幸的是我得到的字符串变成
But unfortunately my result string becomes
msg = "�ä�ö�õ�ü
我在做什么错在这里?
What I'm doing wrong here?
感谢您的回答,我很抱歉,如果我要求明显。
Thank You for your answers and I'm sorry if I'm asking obvious.
推荐答案
使用<一个href="http://msdn.microsoft.com/en-us/library/system.text.encoding.convert.aspx">Encoding.Convert试图德丙它$ C $进入你的目的地编码之前调整字节数组。
Use Encoding.Convert to adjust the byte array before attempting to decode it into your destination encoding.
Encoding iso = Encoding.GetEncoding("ISO-8859-1");
Encoding utf8 = Encoding.UTF8;
byte[] utfBytes = utf8.GetBytes(Message);
byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
string msg = iso.GetString(isoBytes);
这篇关于C#转换字符串从UTF-8到ISO-8859-1(Latin1的)H的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!