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

14 .net c# compact-framework

请参阅主题,请注意,此问题仅适用于.NET compact框架.这种情况发生在Windows Mobile 6 Professional SDK附带的模拟器以及我的英文HTC Touch Pro(所有.NET CF 3.5)上.iso-8859-1代表西欧(ISO),它可能是除了us-ascii之外最重要的编码(至少当一个用户网帖的数量).

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

  • iso-8859-2(中欧(ISO))
  • iso-8859-3(Latin 3(ISO))
  • iso-8859-4(波罗的海(ISO))
  • iso-8859-5(西里尔文(ISO))
  • iso-8859-7(希腊语(ISO))

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

谢谢!

安德烈亚斯

spl*_*tne 15

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

System.Text.Encoding.GetEncoding(1252)
Run Code Online (Sandbox Code Playgroud)

  • 嗨这种方法在某些情况下非常麻烦,例如,当您有一个带有ISO-8859-1编码的XML流时.在将其提供给XML阅读器之前,您需要将编码"iso-8859-1"替换为"windows-1252". (3认同)

Oti*_*tis 7

这篇MSDN文章说:

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

对代码页编码的支持有限,并且只有在设备的操作系统识别编码时才有.

如果设备上没有所需的编码,.NET Compact Framework将抛出PlatformNotSupportedException.

我相信所有(或至少很多)ISO编码都是代码页编码,属于"有限支持"规则.作为替代品,UTF8可能是您最好的选择.


hdk*_*rus 5

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

namespace System.Text
{
    public class Latin1Encoding : Encoding
    {
        private readonly string m_specialCharset = (char) 0xA0 + @"¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ";

        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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)