C# System.Text.Encoding.GetEncoding("iso-8859-1") 抛出 PlatformNotSupportedException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/398621/
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 01:55:58  来源:igfitidea点击:

System.Text.Encoding.GetEncoding("iso-8859-1") throws PlatformNotSupportedException?

c#.netcompact-framework

提问by

See subject, note that this question only applies to the .NET compactframework. This happens on the emulators that ship with Windows Mobile 6 Professional SDK as well as on my English HTC Touch Pro (all .NET CF 3.5). iso-8859-1 stands for Western European (ISO), which is probably the most important encoding besides us-ascii (at least when one goes by the number of usenet posts).

请参阅主题,请注意此问题仅适用于 .NET紧凑型框架。这发生在 Windows Mobile 6 Professional SDK 附带的模拟器以及我的英文版 HTC Touch Pro(全部为 .NET CF 3.5)上。iso-8859-1 代表西欧 (ISO),这可能是除 us-ascii 之外最重要的编码(至少在按 usenet 帖子数量计算时)。

I'm having a hard time to understand why this encoding is not supported, while the following ones are supported (again on both the emulators & my HTC):

我很难理解为什么不支持这种编码,而支持以下编码(同样在模拟器和我的 HTC 上):

  • iso-8859-2 (Central European (ISO))
  • iso-8859-3 (Latin 3 (ISO))
  • iso-8859-4 (Baltic (ISO))
  • iso-8859-5 (Cyrillic (ISO))
  • iso-8859-7 (Greek (ISO))
  • iso-8859-2(中欧 (ISO))
  • iso-8859-3(拉丁语 3 (ISO))
  • iso-8859-4(波罗的海 (ISO))
  • iso-8859-5(西里尔字母 (ISO))
  • iso-8859-7(希腊语 (ISO))

So, is support for say Greek more important than support for German, French and Spanish? Can anyone shed some light on this?

那么,支持说希腊语比支持德语、法语和西班牙语更重要吗?任何人都可以对此有所了解吗?

Thanks!

谢谢!

Andreas

安德烈亚斯

回答by Otis

This MSDN articlesays:

这篇 MSDN 文章说:

The .NET Compact Framework supports character encoding on all devices: Unicode (BE and LE), UTF8, UTF7, and ASCII.

There is limited support for code page encoding and only if the encoding is recognized by the operating system of the device.

The .NET Compact Framework throws a PlatformNotSupportedException if the a required encoding is not available on the device.

.NET Compact Framework 支持所有设备上的字符编码:Unicode(BE 和 LE)、UTF8、UTF7 和 ASCII。

对代码页编码的支持有限,并且仅当设备的操作系统识别编码时。

如果所需的编码在设备上不可用,.NET Compact Framework 将引发 PlatformNotSupportedException。

I believe all (or at least many) of the ISO encodings are code-page encodings and fall under the "limited support" rule. UTF8 is probably your best bet as a replacement.

我相信所有(或至少许多)ISO 编码都是代码页编码并且属于“有限支持”规则。UTF8 可能是您最好的替代选择。

回答by splattne

I would try to use "windows-1252" as encoding string. According to Wikipedia, Windows-1252 is a superset of ISO-8859-1.

我会尝试使用“windows-1252”作为编码字符串。根据维基百科,Windows-1252 是 ISO-8859-1 的超集。

System.Text.Encoding.GetEncoding(1252)

回答by ctacke

It is odd that 8859-1 isn't supported, but that said, UTF-8 does have the ability to represent all of teh 8859-1 characters (and more), so is there a reason you can't just use UTF-8 instead? That's what we do internally, and I just dealt with almost this same issue today. The plus side of using UTF-8 is that you get support for far-east and cyrillic languages without making modifications and without adding weight to the western languages.

不支持 8859-1 很奇怪,但话虽如此,UTF-8 确实能够表示所有 8859-1 字符(以及更多),所以有什么理由不能只使用 UTF- 8代替?这就是我们内部所做的,我今天刚刚处理了几乎同样的问题。使用 UTF-8 的好处是,您无需进行修改,也无需增加西方语言的权重即可获得对远东和西里尔语言的支持。

回答by hdkrus

I know its a bit later but I made an implementation for .net cf of encoding ISO-8859-1, I hope this could help:

我稍后知道它,但我为编码 ISO-8859-1 的 .net cf 做了一个实现,我希望这可以帮助:

namespace System.Text
{
    public class Latin1Encoding : Encoding
    {
        private readonly string m_specialCharset = (char) 0xA0 + @"?¢£¤¥|§¨?a??-?ˉ°±23′μ?·?1o?????àá??????èéê?ìí??D?òó???×?ùú?üYT?àáa?????èéê?ìí??e?òó???÷?ùú?üyt?";

        public override string WebName
        {
            get { return @"ISO-8859-1"; }
        }

        public override int CodePage
        {
            get { return 28591; }
        }

        public override int GetByteCount(char[] chars, int index, int count)
        {
            return count;
        }

        public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
        {
            if (chars == null)
                throw new ArgumentNullException(@"chars", @"null array");
            if (bytes == null)
                throw new ArgumentNullException(@"bytes", @"null array");
            if (charIndex < 0)
                throw new ArgumentOutOfRangeException(@"charIndex");
            if (charCount < 0)
                throw new ArgumentOutOfRangeException(@"charCount");
            if (chars.Length - charIndex < charCount)
                throw new ArgumentOutOfRangeException(@"chars");
            if (byteIndex < 0 || byteIndex > bytes.Length)
                throw new ArgumentOutOfRangeException(@"byteIndex");

            for (int i = 0; i < charCount; i++)
            {
                char ch = chars[charIndex + i];
                int chVal = ch;
                bytes[byteIndex + i] = chVal < 160 ? (byte)ch : (chVal <= byte.MaxValue ? (byte)m_specialCharset[chVal - 160] : (byte)63);
            }

            return charCount;
        }

        public override int GetCharCount(byte[] bytes, int index, int count)
        {
            return count;
        }

        public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
        {
            if (chars == null)
                throw new ArgumentNullException(@"chars", @"null array");
            if (bytes == null)
                throw new ArgumentNullException(@"bytes", @"null array");
            if (byteIndex < 0)
                throw new ArgumentOutOfRangeException(@"byteIndex");
            if (byteCount < 0)
                throw new ArgumentOutOfRangeException(@"byteCount");
            if (bytes.Length - byteIndex < byteCount)
                throw new ArgumentOutOfRangeException(@"bytes");
            if (charIndex < 0 || charIndex > chars.Length)
                throw new ArgumentOutOfRangeException(@"charIndex");

            for (int i = 0; i < byteCount; ++i)
            {
                byte b = bytes[byteIndex + i];
                chars[charIndex + i] = b < 160 ? (char)b : m_specialCharset[b - 160];
            }

            return byteCount;
        }

        public override int GetMaxByteCount(int charCount)
        {
            return charCount;
        }

        public override int GetMaxCharCount(int byteCount)
        {
            return byteCount;
        }
    }
}

回答by Prasad KM

If anyone is getting an exception(.NET compact framework) like:

如果有人遇到异常(.NET 紧凑型框架),例如:

 System.Text.Encoding.GetEncoding(“iso-8859-1”) throws PlatformNotSupportedException,

//Please follow the steps: 

   byte[] bytes=Encoding.Default.GetBytes(yourText.ToString);

//The code is:

    FileInfo fileInfo = new FileInfo(FullfileName); //file type :  *.text,*.xml 
    string yourText = (char) 0xA0 + @"?¢£¤¥|§¨?a??-?ˉ°±23′μ?·?1o?????àá??????èéê?ìí??D?òó???×?ùú?üYT?àáa?????èéê?ìí??e?òó???÷?ùú?üyt?";

    using (FileStream s = fileInfo.OpenWrite()) {
                        s.Write(Encoding.Default.GetBytes(yourText.ToString()), 0, yourText.Length);
    }