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的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在谷歌上搜索过这个话题,也看过每一个答案,但我还是没明白.
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 并且我使用以下代码进行:
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));
我的源字符串是
Message = "ÄäÖöÕõÜü"
但不幸的是我的结果字符串变成了
But unfortunately my result string becomes
msg = "�ä�ö�õ�ü
我在这里做错了什么?
推荐答案
使用 Encoding.Convert 在尝试将其解码为目标编码之前调整字节数组.
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屋!
查看全文