C#转换字符串从UTF-8到ISO-8859-1(Latin1的)H字符串、UTF、ISO

由网友(执刀手)分享简介:我知道这已经问过!我用Google搜索这个话题,我已经看过所有的答案,但我还是不明白这一点。I have googled on this topic and I have looked at every answer, but I still don't get it.基本上我需要为UTF-8字符串转换为ISO-...

我知道这已经问过!

我用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.

推荐答案

使用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);
阅读全文

相关推荐

最新文章