C#将字符串从UTF-8转换为ISO-8859-1(Latin1)H [英] C# Convert string from UTF-8 to ISO-8859-1 (Latin1) H
本文介绍了C#将字符串从UTF-8转换为ISO-8859-1(Latin1)H的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基本上我需要转换UTF-8字符串到ISO-8859-1,我使用以下代码:
编码iso = Encoding.GetEncoding(ISO-8859- 1\" );
编码utf8 = Encoding.UTF8;
string msg = iso.GetString(utf8.GetBytes(Message));
我的源字符串是
Message =ÄäÖöÕõÜü
但不幸的是,我的结果字符串变成
msg =Ã?Ã? pre>
我在这里做错什么?
解决方案使用 Encoding.Convert 以调整字节数组之前尝试将其解码为目标编码。
编码iso = Encoding.GetEncoding(ISO-8859-1);
编码utf8 = Encoding.UTF8;
byte [] utfBytes = utf8.GetBytes(Message);
byte [] isoBytes = Encoding.Convert(utf8,iso,utfBytes);
string msg = iso.GetString(isoBytes);
I have googled on this topic and I have looked at every answer, but I still don't get it.
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?
解决方案 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屋!
查看全文