C# 如何将字符串转换为“iso-8859-1”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/629617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:59:41  来源:igfitidea点击:

How to convert string to "iso-8859-1"?

c#asp.net

提问by Tom

How can i convert an UTF-8 string into an ISO-8859-1 string?

如何将 UTF-8 字符串转换为 ISO-8859-1 字符串?

采纳答案by John Feminella

Try:

尝试:

        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);

回答by Mehrdad Afshari

.NET Strings are all UTF-16 internally. There is no UTF-8 or ISO-8859-1 encoded System.Stringin .NET. To get the binary representation of the string in a particular encoding use System.Text.Encodingclass:

.NET 字符串在内部都是 UTF-16。System.String.NET 中没有 UTF-8 或 ISO-8859-1 编码。要获取特定编码使用System.Text.Encoding类中字符串的二进制表示:

byte[] bytes = Encoding.GetEncoding("iso-8859-1").GetBytes("hello world");