如何将UTF-8字符串转换为ISO-8859-1字符串?
尝试:
System.Text.Encoding iso_8859_1 = System.Text.Encoding.GetEncoding("iso-8859-1"); System.Text.Encoding utf_8 = System.Text.Encoding.UTF8; // Unicode string. string s_unicode = "abcéabc"; // Convert to ISO-8859-1 bytes. byte[] isoBytes = iso_8859_1.GetBytes(s_unicode); // Convert to UTF-8. byte[] utf8Bytes = System.Text.Encoding.Convert(iso_8859_1, utf_8, isoBytes);
.NET字符串在内部都是UTF-16.System.String
.NET中没有UTF-8或ISO-8859-1编码.要在特定编码使用System.Text.Encoding
类中获取字符串的二进制表示:
byte[] bytes = Encoding.GetEncoding("iso-8859-1").GetBytes("hello world");